Generating the clientside Java stubs

In this section, we will create the Java clientside stub classes that encapsulate the Web service operations published via a WSDL. JDK 1.6 ships with a compiler "wsimport", that can parse a WSDL document and generate Java classes that expose all web service operations via an interface. Remember, this tool needs only a WSDL, and does not depend on the technology that the server has used.

The syntax of wsimport is wsimprt [options] <WSDL_URI>

Some of the most commonly used options are:

For example,

wsimport -d src -p com.vinod.stubs -keep -verbose http://localhost:9999/customerService?WSDL

The following picture is the output generated when the wsimport command was issued at the command prompt from the "src" directory of an Eclipse project.

Using wsimport command to generate stubs

The WSDL used here is the one we got in the previous example. The wsimoprt command resulted in the following classes (and interfaces) worth mentioning.

Once these classes are generated, we now write a client application to access the web service. The name of the interface (ICustomerService) is derived from the following piece of WSDL:


<portType name="ICustomerService">

The name of the Service class (CustomerService) is derived from the following:


<service name="CustomerService">

The Service class (CustomerService) will have a method with getXXX style, where XXX correspond to the name attribute used in the <port> tag inside the <service> tag.


<port name="CustomerServicePort">

So, inorder to make a SOAP request using these stubs, we do the following:

  1. Create a variable of service interface
  2. Create an object the service class
  3. Call the getXXX method to get a port for communication, and assign this to the service interface variable
  4. Call the web service operation using the interface method

Here is a sample

ICustomerService service;
CustomerService cs=new CustomerService();
service=cs.getCustomerServicePort();
CustomerType cust=cs.getCustomerData();
// process the "cust"

Here is the complete code:

package com.vinod.client;

import com.vinod.stubs.CustomerService;
import com.vinod.stubs.CustomerType;
import com.vinod.stubs.ICustomerService;

public class CustomerClient {
	public static void main(String[] args) {
		ICustomerService service;
		CustomerService cs=new CustomerService();
		service = cs.getCustomerServicePort();
		CustomerType cust = service.getCustomerData();
		System.out.println("Got customer data: ");
		System.out.printf("Name\t: %s\n", cust.getCustomerName());
		System.out.printf("Address\t: %s, %s\n", 
				cust.getCustomerAddress().getCity(), 
				cust.getCustomerAddress().getState());
	}
}


Use the following links to navigate