Java Tutorial - Java Scipt : A Web Service Client Using Generated Stubs

Java Tutorial - Java Scipt :

A Web Service Client Using Generated Stubs


In the first example in the previous section, we had to write a client using a generic interface. Luckily for us Java developers, Apache Axis comes with a Java client stub generator that completes the usability loop on the client side. Given the WSDL for a Web service, the appropriate Java client stubs are generated. In this example, we’ll use the generator to simplify the code of the SimpleShopClient.

The generator program, WSDL2Java, is contained within the wsdl4j.jar file. The only parameter required is the URL pointing to the WSDL description. We’ll be using it on the SimpleShop Web service we deployed earlier on Tomcat. Make sure that Tomcat is running before generating the stubs because we’ll be relying on the WSDL that Apache Axis automatically created from SimpleShop.jws.

 You can also use a WSDL file within the local file system. Just use the appropriate URL( that is, file:///temp/SimpleShop.wsdl).

To run WSDL2Java on Windows, use the following (extremely long) command:

java -classpath .;%TOMCAT_HOME%\webapps\axis\WEBINF\
lib\axis.jar;%TOMCAT_HOME%\webapps\axis\WEBINF\
lib\jaxrpc.jar;%TOMCAT_HOME%\webapps\axis\WEB-INF\lib\wsdl4j.jar;%TO
MCAT_HOME%\webapps\axis\WEB-INF\lib\commonslogging.
jar;%TOMCAT_HOME%\webapps\axis\WEB-INF\lib\commonsdiscovery.
jar;%TOMCAT_HOME%\common\lib\servlets.jar;%TOMCAT_HOME%\webapp
s\axis\WEB-INF\lib\saaj.jar org.apache.axis.wsdl.WSDL2Java

On Linux, use the following command instead:

java -classpath .;%TOMCAT_HOME%\webapps\axis\WEBINF\
lib\axis.jar;%TOMCAT_HOME%\webapps\axis\WEBINF\
lib\jaxrpc.jar;%TOMCAT_HOME%\webapps\axis\WEB-INF\lib\wsdl4j.jar;%TO
MCAT_HOME%\webapps\axis\WEB-INF\lib\commonslogging.
jar;%TOMCAT_HOME%\webapps\axis\WEB-INF\lib\commonsdiscovery.
jar;%TOMCAT_HOME%\common\lib\servlets.jar;%TOMCAT_HOME%\webapp
s\axis\WEB-INF\lib\saaj.jar org.apache.axis.wsdl.WSDL2Java

After the generator is completed, there should be a new folder named localhost in the directory. The generator uses the namespace of the Web service in generating the stubs. In our case, the service is located under the
localhost\axis directory. The package name for the new stubs will match that same hierarchy.

Here’s an example of one of the generated stubs, SimpleShopService:

/**
* SimpleShop.java
*
* This file was auto-generated from WSDL
* by the Apache Axis WSDL2Java emitter.
*/

package localhost.axis.SimpleShop_jws;

public interface SimpleShop extends java.rmi.Remote
{
public float getPrice(java.lang.String sProductName)
throws java.rmi.RemoteException;
}

The resemblance to our source file for SimpleShop.jws is no coincidence. The client stub and the Web service are synchronized through the WSDL description. Using the stubs, we no longer have to set the endpoints, data types, and operation names. An important side effect is that we no longer need to enter the Web service’s URL, which also means that the generated stub only works with one particular endpoint. However, now we can focus on using the
Web service as just another Java object. To illustrate the difference, we’ll create SimpleShopClient2, using the stubs to communicate with our Web service.

Here’s the source code for SimpleShopClient2:

import localhost.axis.SimpleShop_jws.SimpleShop;
import localhost.axis.SimpleShop_jws.SimpleShopService;
import localhost.axis.SimpleShop_jws.SimpleShopServiceLocator;

public class SimpleShopClient2
{
public static void main( String args[] )
{
try
{
SimpleShopService oServiceLocator =
new SimpleShopServiceLocator();
SimpleShop oService = oServiceLocator.getSimpleShop();
float fPrice = oService.getPrice( “coffee cup” );
System.out.println( “The price is “ + fPrice );
}
catch( Exception e )
{
e.printStackTrace();
}
}
}

As you can see, it is much easier to write a Web services client using the generated stub classes. Stubs can be generated for existing public Web services, such as Google’s Web Services API and Amazon.com’s Amazon Web Services 2.0. Integrating these external Web services into your applications is easy and opens up new avenues for collaboration. Following are the URLs to access these services:

·         Google Web Services API:


·         Amazon Web Services 2.0:

Apache Axis is able to generate stubs that work with the ASP.NET Web services without any problems. The Microsoft SOAP Interoperability server (http://mssoapinterop.org/) lets you test cross-platform Web services integration with a .NET server. This is a good place to get more information on interoperability.

Generating client stubs from the WSDL description is just one side of the story. It is also possible to generate the server-side skeleton at the same time as the client stubs. So instead of starting from the .jws file, you would begin by
authoring a description of your Web service. Then, the WSDL file would be used to generate the client stubs and the server-side skeleton for your Web service. This approach is rarely used, so we won’t be covering it in this book. This feature is similar to what we have already seen and more detail is beyond the scope of this book; however, if you are interested, the details are available on the Apache Axis Web site.