Consuming Web Services with Java


In the "Building Web Services with Java" section, you created two different Web services. One using the instant deployment (named HelloWorld.jws) and another one using the advanced deployment approach (named ProductService.java). This section will demonstrate the steps involved in consuming these services from Java client applications. First, let us start with the HelloWorld.jws service.

Consuming the HelloWorld Service

For the purposes of this example, create a new Java file named HelloWorldClient.java and modify its code to look as shown in Listing 19-11.

Listing 19-11: Implementation of client for the product service

image from book
      package com.wrox.webservices;      import org.apache.axis.client.Call;      import org.apache.axis.client.Service;      import org.apache.axis.encoding.XMLType;      import org.apache.axis.utils.Options;      import javax.xml.rpc.ParameterMode;      public class HelloWorldClient      {         public static void main(String [] args) throws Exception {            Options options = new Options(args);            String endpoint = "http://localhost:" + options.getPort() +               "/axis/HelloWorld.jws";            args = options.getRemainingArgs();            if (args == null || args.length != 2) {               System.err.println("Usage: HelloWorldClient <HelloWorld> arg1 ");               return;            }            String method = args[0];            if (!(method.equals("HelloWorld"))){               System.err.println("Usage: HelloWorld arg1 ");               return;            }            String name = new String(args[1]);            Service  service = new Service();            Call call  = (Call) service.createCall();            call.setTargetEndpointAddress( new java.net.URL(endpoint) );            call.setOperationName( method );            call.addParameter( "name", XMLType.XSD_STRING, ParameterMode.IN );            call.setReturnType( XMLType.XSD_STRING );            String result = (String)call.invoke(new Object[] { name });            System.out.println(result);         }      } 
image from book

To start with, create an instance of the org.apache.axis.utils.Options class and pass in the arguments array passed to the main method. Through the getPort() method of the Options object, you retrieve the port number in which the Web service is hosted. In addition to the port number, the input arguments passed to the main method also include the name of the method as well as the argument to be passed to the method. You retrieve these two values and assign them to local variables for later use.

Then you create a new Service instance and then use the Service object to create a service call through the createCall() method. In this case, you create it directly rather than using a ServiceFactory because Axis only supports JAX-RPC calls. This approach enables you to keep everything generic because of which the HelloWorldClient doesn't need to know anything about the HelloWorld service and let the Web service framework handle the underlying complexities.

      Service  service = new Service();      Call call  = (Call) service.createCall(); 

Next, you specify the location of the Web service using the setTargetEndpointAddress() method, to which you pass in the endpoint of the Web service.

      call.setTargetEndpointAddress( new java.net.URL(endpoint) ); 

Here you define the operation name to execute through the call to the setOperationName() method.

      call.setOperationName( method ); 

Here you tell the service call that you have an input parameter and an output parameter. You set the input parameter name to name and map its String type to the corresponding XSD type. Since the output parameter is of String type, you set the return type to XSD_STRING.

      call.addParameter( "name", XMLType.XSD_STRING, ParameterMode.IN );      call.setReturnType( XMLType.XSD_STRING ); 

Then you invoke the Web service through the Call.Invoke() method call passing in the name input parameter.

      String result = (String)call.invoke(new Object[] { name }); 

Finally, you capture the response returned from the service and typecast that into a String. You then display the value of the result variable by calling the System.out.println() method.

      System.out.println(result); 

Running the Client Application

Now that you have created the client application, you are ready to run the application. First, compile the HelloWorldClient.java using the compiler. After that, enter the following command from the command prompt to initiate the client application.

      java com.wrox.webservices.HelloWorldClient -p8080 HelloWorld "Thiru" 

In the previous command:

  • q The switch p specifies the port number

  • q HelloWorld indicates the name of the Web service method to be invoked

  • q “Thiru” is the argument passed to the HelloWorld method

The result of the previous command displays output similar to Figure 19-18:

image from book
Figure 19-18

Consuming the ProductService

In this section, you will see the steps involved in consuming the ProductService. Remember that the ProductService has a method named getProduct that accepts a productID as an argument and returns the details of that product in the form of a Product object. To start with, create a new Java class named ProductClient and add the code shown in Listing 19-12 to it.

Listing 19-12: Implementation of Cllent for the Product Service

image from book
      package com.wrox.webservices;      import org.apache.axis.AxisFault;      import org.apache.axis.client.Call;      import org.apache.axis.client.Service;      import org.apache.axis.utils.Options;      import javax.xml.namespace.QName;      import javax.xml.rpc.ParameterMode;      public class ProductClient      {         public static void main(String [] args) throws Exception         {            Options options = new Options(args);            Service  service = new Service();            Call call = (Call) service.createCall();            QName qn = new QName( "urn:ProductService", "Product" );            call.registerTypeMapping(Product.class, qn,               new org.apache.axis.encoding.ser.BeanSerializerFactory(Product.class, qn),               new org.apache.axis.encoding.ser.BeanDeserializerFactory(Product.class,               qn));            Product result = null;            try {               call.setTargetEndpointAddress( new java.net.URL(options.getURL()) );               call.setOperationName( new QName("ProductProcessor", "getProduct") );               call.addParameter("productID", org.apache.axis.encoding.XMLType.XSD_INT,                  ParameterMode.IN);               call.setReturnType(org.apache.axis.encoding.XMLType.XSD_ANYTYPE);               result = (Product) call.invoke( new Object[] { 1 } );            }            catch (AxisFault fault) {                System.out.println("Error : " + fault.toString());            }            System.out.println("Name : " + result.getName());            System.out.println("Product ID : " + result.getProductID());            System.out.println("Product Number : " + result.getProductNumber());            System.out.println("Colr : " + result.getColor());         }      } 
image from book

To start with, create a new Service instance and then as the Service to create a call through the createCall() method.

      Service  service = new Service();      Call call = (Call) service.createCall(); 

To help us manage the command line options, Apache provides the following class: org.apache .axis.utils.Options. From this class, you can extract the URL of the destination Web Service through its getURL() method. Then you set the setTargetEndpointAddress() method of the Call object to the target endpoint to connect:

      call.setTargetEndpointAddress( new java.net.URL(options.getURL()) ); 

Next, you define the operation name to execute; this involves the creation of a QName. The QName uses the ProductService namespace (that was specified in the deploy.wsdd file) and the name of the method on the ProductService that you want to execute, which is getProduct in this case.

      call.setOperationName( new QName("ProductProcessor", "getProduct") ); 

Here you tell the call that you have an input parameter and an output parameter. You set the input parameter name to productID and map its int type to the corresponding XSD type. Since the output parameter is an object of type Product, you set the return type to XSD_ANYTYPE.

      call.addParameter("productID", org.apache.axis.encoding.XMLType.XSD_INT,         ParameterMode.IN);      call.setReturnType(org.apache.axis.encoding.XMLType.XSD_ANYTYPE); 

Finally, you invoke the Web service through the Call.Invoke() method call passing in the input parameters as an object array.

      result = (Product) call.invoke( new Object[] { 1 } ); 

Note that in the previous line of code, you set the product id to a hard-coded value, which is 1 in this case. You capture the response returned from the service and typecast that into a Product object. You then display the values contained in the Product object through the System.out.println() method.

Running the Client Application

After compiling the ProductClient.java, enter the following command from the command prompt.

      java com.wrox.webservices.ProductClient -llocal:// 

You should see an output similar to Figure 19-19.

image from book
Figure 19-19




Professional XML
Professional XML (Programmer to Programmer)
ISBN: 0471777773
EAN: 2147483647
Year: 2004
Pages: 215

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net