Creating a Simple Web Service Client in Java

image

Even though Web Services have been around for a while (a long while at that), I somehow haven't had the opportunity to actually incorporate any into an application that I have worked on. You may be in the same boat, and so for your edification (and mine) I will walk you through creating a simple client which queries a web service using Java. Here goes nothing. The first step would be to identify a web service that you'd like to use in your application. It could be one that was created by another team in your organization, however in this case we'll just pick one from a list of freely available web services located at www.xmethods.org. For this exercise I will be using the service called "US Zip Validator". You can see the details for this service by clicking "Full List" and doing a quick browser text search. The WSDL for this service can be viewed at http://www.w3schools.com/xml/xml_wsdl.asp. This service, given a US zip code, returns a State Abbreviation as well as the latitude and longitude (of the "center" of the zip code). The next step will be to use the Java "wsimport" tool that ships with your JDK (Java Development Kit) to generate the stub classes that will be used send and receive SOAP messages from the Web Service. I'm using JDK version 1.6.0_33 but I believe JDK 1.5+ ships with this tool. If you don't have a recent JDK installed maybe it's time to upgrade? Next, run the following command: wsimport -s src -d bin http://www.webservicemart.com/uszip.asmx?WSDL This command produces Java source code in the /src directory and compiled .class files in the /bin directory relative to the directory from which the command was issued. These generated classes will be used to access the remote web service. Create a jar file,  zipws.jar, containing the classes in the package com.webservicemart.ws by issuing the following commands:

  1. cd /bin
  2. jar cvf zipws.jar com

Then fire up Eclipse and create a new "Java Project" named "ZipWS". Make sure to add the newly created zipws.jar as well as the required jars for the client code. JDK 1.6 includes all the necessary jars required to run this code. If you are using an earlier JDK version, you can check Jar Finder to determine which jars contain the necessary packages in the import statements on lines 2-5 below. Once your development environment is ready to go, create a Java Class called "ZipWSTest" with the following code to execute the web service and extract the important bits of information it returns.

Lines 8-10 depicted above, instantiate classes that utilize JAX-WS (Java API for XML Web Services) created by the wsimport tool, and call the web service (USZip.validateZip) with a hard coded sample zip code. For this call, it turns out that the web service returns a single xml node : <item state="[state abbreviation]" latitude="[lat]" longitude="[long]"/> . So I need to pad the response with markup (line 12) to make the string a well-formed XML document ready for parsing. The remainder of the code (lines 15-31) simply parses the XML into an in-memory tree and retrieves the attribute values to print to the console. The final output can be seen in the Eclipse console window in the screen shot just below.


I hope you found this quick tutorial useful. Until next time! -Mitul

Subscribe to Our Newsletter

Stay In Touch