Consuming a web service using JAX-WS

Let's now create a simple Java console app to consume the web service we created earlier. Select File | New | Maven Project. Select the Create a simple project option on the first page and click Next. Enter the following configuration details:

Figure 9.10: Create a Maven project for the web service client

Make sure that the Packaging type is jar. Click Finish.

We will now generate a stub and a supporting class on the client side for invoking the web service. We will use the wsimport tool to generate client classes. We will specify the package for the generated classes by using the -p option and the WSDL location to generate client classes. The wsimport tool is part of the JDK and should be available in the <JDK_HOME>/bin folder, if you are using JDK 1.7 or later.

Change the folder to <project_home>/src//main/java and run the following command:

wsimport -keep -p packt.jee.eclipse.ws.soap.client http://localhost:8080/CourseMgmtWSProject/courseService?wsdl
  

The -keep flag instructs wsimport to keep the generated file and not delete it.

The -p option specifies the package name for the generated classes.

The last argument is the WSDL location for the web service. In Package Explorer or Project Explorer of Eclipse, refresh the client project to see the generated files. The files should be in the packt.jee.eclipse.ws.soap.client package.

wsimport generates a client-side class for each type defined in the schema (in the types element of WSDL). Therefore, you will find Course, GetCourse, GetCourseResponse, GetCourses, and GetCoursesResponse classes. Furthermore, it generates classes for the portType (CourseManagementService) and service (CourseManagementServiceService) elements of the WSDL. Additionally, it creates an ObjectFactory class that creates Java objects from XML using JAXB.

Let's now write the code to actually call the web service. Create the CourseMgmtWSClient class in the packt.jee.eclipse.ws.soap.client.test package:

package packt.jee.eclipse.ws.soap.client.test; 
 
import packt.jee.eclipse.ws.soap.client.Course; 
import packt.jee.eclipse.ws.soap.client.CourseManagementService; 
import packt.jee.eclipse.ws.soap.client.CourseManagementServiceService; 
 
public class CourseMgmtWSClient { 
 
  public static void main(String[] args) { 
    CourseManagementServiceService service = new 
CourseManagementServiceService(); CourseManagementService port =
service.getCourseManagementServicePort(); Course course = port.getCourse(1); System.out.println("Course name = " + course.getName()); } }

We first create the Service object and then get the port from it. The port object has operations defined for the web service. We then call the actual web service method on the port object. Right-click on the class and select Run As | Java Application. The output should be the name of the course that we hardcoded in the web service implementation, which is Course-1.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.131.47