Saturday, April 09, 2011

WSDL2JAVA for developing Web Services Client

Its very convinent to create a Web Service Client using the tool WSDL2JAVA. Below are the steps to create one in Eclipse.

Step-1. Create a project in Eclipse ( we will call it testWSClient )

Step-2. Create a new run time configuration in Eclipse by clicking on “Run” and the new run time configuration Icon on the pop-up dialog box.

    1. Main Tab –
      1. In the new Run-time config’s “Main” Tab enter “org.apache.axis.wsdl.WSDL2Java” for Main Class Test box
      2. Enter or Browse Project name “testWSClient”
      3. Type some name for this config in the name text box ( ex – runwsdl2java )
    2. Arguments Tab -
      1. Enter the following in the Argument Tab - http://machinename.companyname.com:8080/module/services/servicemodule/MyService?wsdl -o src - This is the URL of the Web Service for which you want to generate Java code, and the command ” -o src” instructs wsdl2java to do just that
    3. Class Path Tab -
      1. Make sure you have actvitation.jar, axis.jar, common-discovery.jar, common-logging.jar, commons-net.1.3.0.jar, javamail1.4.jar, jaxrpc.jar, saaj.jar and wsdl4j.jar in the classpath of your project and run time config

Step-3 - Now Run the runwsdl2java by selecting it from the Run dropdown, you should see the following

  1. Objects with package details being created for “ComplexTypes” in the WSDL, these are plan data objects
  2. A Port Type Interface with Methods corrspoinding to “Port Types” created
  3. Service and ServiceLocator classes created with methods
    1. getServiceHttpPort(java.net.URL portAddress)
    2. getServiceHttpPort()
  4. A HTTP Binding Stub Class

Note : It should be noted that all these classes are generated automatically by the wsdl2java tool.

Step-4 - Now Write your Client to invoke and access the methods in the WSDL, and interact with the web services.

NOte : Before we start make sure that the “ServiceHttpPort_address” variable in ServiceLocator class is pointing to the correct WSDL URL, which you provided when you were running the wsdl2java, it normally will be, but this is just a check, might not be required.

Now you can write a client, example below

public static void main(String[] args) throws MalformedURLException
{

Service service = new ServiceLocator();
try {
System.out.println(service.getServiceHttpPort().xyzMethod(getMyObject()));
}
catch (Exception e) {
System.out.println(e.toString());
}
}

private MyObject getMyObject() {

// Note this MyObject will be a complexType in the WSDL and generated by the wsdl2java tool, we are creating it to pass it to the method xyzMethod ( also in WDL as a Port Type and acccessible thru ServiceLocater) which takes MyObject as a parameter

MyObject obj = new MyObject( someparameter);

}

No comments: