Sunday 21 February 2010

BCLService - Accessing the web service with a generated client

On my previous post, I explained how you can expose your existing java API as a SOAP web sevice using Apache Axis2. I went through the steps of creating a web service called "BCLService"...Now here's the sequel, we are going to look into how we can interact with the service using a generated client.

This is a step by step guide to interacting with the BCLService using a client based on generated code. This guide uses Axis 2 as an implementation of the SOAP protocol. Axis is a framework for constructing SOAP clients and servers.

1. Requirements:
JDK 1.5 (or latest)
Axis 2 1.4 (or latest)

You can download the latest release of Axis2 from the Apache download site

Apache provides a tool in the Axis2 distribution (WSDL2Java) to generate client side stubs that interacts with the service (You can find WSD2Java in "org.apache.axis.wsdl.WSDL2Java").

2. Generating client stubs using the Apache WSDL2Java tool: Client programs need only to know the EPR and the operation name (both listed on the WSDL) in order to interact with your web service.

In order to generate the stubs, run the following command:

•[Linux]
$ sh WSDL2Java.sh -uri http://:/axis2/services/?wsdl -o /path/to/your/client/code/
•[Windows]
$ WSDL2Java.bat -uri http://:/axis2/services/?wsdl -o /path/to/your/client/code/
-uri Specifies the location of the WSDL file. In other terms, the Endpoint Reference (EPR) of the service with the “?wsdl” argument appended in the end.
-o Specifies the output path for the tool, in other words, the location where you want WSD2Java to generate the client code, Note that you need not to specify the source folder (/src) in the parameter as the source folder will already be added by WSDL2Java

The above command generates 2 java classes. Now, all you need to do is to create a simple class (i.e. Client.java) which uses the stub to call the service.


3. Calling the Web Service: The following is a sample client class code used to interact with the web service.
Make sure to add all the Axis JARs that the stubs refer to (JARs can be found under AXIS_HOME/axis2-1.4/lib).


public class Client {
  public static void main(String[] args)
                           throws RemoteException {
    MyServiceStub stub = new MyServiceStub();

    /* Invoking the GetCurrentPosition service */

    //Create the request
    MyServiceStub.GetCurrentPosition request =
                 new MyServiceStub.GetCurrentPosition ();

    /* if the method doesn’t take parameters, then just
     skip the request object and instantiate the reponse
     object directly by calling the method on the stub */
    request.setClubName("flamengo");

    GetCurrentPosition Response response =
                 stub.GetCurrentPosition(request);

    System.out.println("Web Service returned : " +
                               response.get_return());
  }
}

Now you can compile and run the client code. Its is important to note that the classpath must have all the jars in the "lib" directory of the axis2 distribution when compiling and running the client.

No comments:

Post a Comment