«^»
8.1. Providing a program that accesses a Web Service

Even though we have not yet produced a Web Service, we can use the Axis that we have just installed in order to access an external Web Service. We will suppose that we want to produce a program that contacts the Web Service (used earlier) that translates a zipcode into a place name.

Earlier, we stored the WSDL document of this Web Service in the file ZipService.wsdl. We now execute a program called WSDL2Java on this ZipService.wsdl file:

java org.apache.axis.wsdl.WSDL2Java ZipService.wsdl
As this is a program that comes with Axis, in order to execute the above command you will need to set up the very long classpath that was given earlier.

The WSDL2Java program looks at the WSDL document to discover things about the Web Service. It then generates a proxy class (aka a stub class). For each method of the Web Service, the proxy class will have a method that has the same parameters and the same return type. The idea is that if a client wants to call a method of the Web Service it instead calls the method of the proxy class that has the same name and parameters. Behind the scenes, the call of a method of the proxy class creates the HTTP request (with its complicated SOAP); sends it to the webserver providing the Web Service; waits for the reply; and then decodes the SOAP that is returned by the Web Service.

If, having executed the above WSDL2Java command, you then execute the command:

dir org\dyndns\dinoch\webservices
you will see that the WSDL2Java program has created the files:
ArrayOfString.java
Latitude.java
LatLong.java
Longitude.java
ZipcodeLookupService.java
ZipcodeLookupServiceLocator.java
ZipcodeLookupServiceSoap.java
ZipcodeLookupServiceSoapStub.java
The file ZipcodeLookupServiceSoapStub.java contains the proxy class; the other files are supporting files.

The interface ZipcodeLookupService declares headers for get methods for each port that is listed in the service element of the WSDL document. In our example, it declares a header for a method called getZipcodeLookupServiceSoap. The class ZipcodeLookupServiceLocator is an implementation of this interface, and so it implements the getZipcodeLookupServiceSoap method. This method returns an instance of the proxy class ZipcodeLookupServiceSoapStub. This class implements the interface ZipcodeLookupServiceSoap: this interface just declares headers for each method that is provided by the Web Service.

Now we can produce a Java program that is going to be a client of this Web Service:

0191: import java.rmi.                      RemoteException;  // MyTestClient.java
0192: import javax.xml.rpc.                 ServiceException;
0193: import org.dyndns.dinoch.webservices. ZipcodeLookupService;
0194: import org.dyndns.dinoch.webservices. ZipcodeLookupServiceLocator;
0195: import org.dyndns.dinoch.webservices. ZipcodeLookupServiceSoap;
0196: public class MyTestClient
0197: {
0198:    public static void main(String[] pArgs) throws RemoteException, ServiceException
0199:    {
0200:       String tZipcodeString = pArgs[0];
0201:       ZipcodeLookupService tZipcodeLookupService = 
0202:             new ZipcodeLookupServiceLocator();
0203:       ZipcodeLookupServiceSoap tZipcodeLookupServiceSoap = 
0204:             tZipcodeLookupService.getZipcodeLookupServiceSoap();
0205:       String tCityAndStateString = 
0206:             tZipcodeLookupServiceSoap.zipTo1CityAndState(tZipcodeString);
0207:       System.out.println(tCityAndStateString);
0208:    }
0209: }

MyTestClient.java can be compiled and run using a particular zipcode by the commands:

javac MyTestClient.java
java MyTestClient 94042
The program should contact the Web Service and then output MOUNTAIN VIEW CA. Note: if your PC needs to use a proxy server to get to the internet, you will need a java command like:
java -Dhttp.proxyHost=proxy.site.com -Dhttp.proxyPort=8080 MyTestClient 94042