2009-08-06

Quick Building a Web service client using Axis

After some studies, to my surprise, building a web service client using the Axis library (not the web service itself) is in fact very easy. I would like to summarize my experience in this blog.

  1. Download Axis from the Apache web site and expand the zip file (Remark: in fact the whole package includes the server code. To build a client, only the jar files are needed. But for simplicity and because of the limited extra hard disk space, I recommend to expand the whole package).
    In my example, I have downloaded Axis version 1.1 and put it at /home/craft/whc/axis-1-1

  2. Download the WSDL file of the web service. In this case, I have chosen the free stock quote example at webservicex at http://www.webservicex.net/stockquote.asmx?wsdl and store it in /home/craft/whc/StockQuote.wsdl

  3. Use the WSDL2Java tool from Axis to crate the stub codes. Since we need to refer to the jar files in the Axis, I have built a script with classpath set appropriately
    #!/bin/sh
    java -classpath "/home/craft/whc/axis-1_1/lib/axis.jar:
    /home/craft/whc/axis-1_1/lib/log4j-1.2.8.jar:
    /home/craft/whc/axis-1_1/lib/commons-logging.jar:
    /home/craft/whc/axis-1_1/lib/commons-discovery.jar:
    /home/craft/whc/axis-1_1/lib/wsdl4j.jar:
    /home/craft/whc/axis-1_1/lib/jaxrpc.jar:
    /home/craft/whc/axis-1_1/lib/saaj.jar"
    org.apache.axis.wsdl.WSDL2Java StockQuote.wsdl
    The resultant files will be created at /home/craft/whc/NET/webserviceX/www

  4. Create the simple client code (StockClient.java) as follows (I have hard-coded MSFT (Microsoft) for the stock code)
    import java.net.URL;
    import org.apache.axis.client.Service;
    import org.apache.axis.client.Call;

    public class StockClient {
    public static void main(String[] args) throws Exception {

    NET.webserviceX.www.StockQuote service = new NET.webserviceX.www.StockQuoteLocator();

    NET.webserviceX.www.StockQuoteSoap port = service.getStockQuoteSoap();
    String result = port.getQuote("MSFT");
    System.out.println("Result = "+result);
    }
    }

  5. Compile the client code with a script as follows (Please note that I have deliberately omitted the wsdl4j.jar because the wsdl file is no longer processed at run time and ass a dot (current directory in the classpath)
    #!/bin/sh
    javac -source 1.5 -classpath "/home/craft/whc/axis-1_1/lib/axis.jar:
    /home/craft/whc/axis-1_1/lib/log4j-1.2.8.jar:
    /home/craft/whc/axis-1_1/lib/commons-logging.jar:
    /home/craft/whc/axis-1_1/lib/commons-discovery.jar:
    /home/craft/whc/axis-1_1/lib/jaxrpc.jar:
    /home/craft/whc/axis-1_1/lib/saaj.jar:." StockClient.java

  6. Run the complied class file (again, I have used a script to add the classpath. Moreover, since we need a http proxy to access the internet, I have defined these properties in during invoking the JVM using the -D switch
    #!/bin/sh
    java -classpath "/home/craft/whc/axis-1_1/lib/axis.jar:
    /home/craft/whc/axis-1_1/lib/log4j-1.2.8.jar:
    /home/craft/whc/axis-1_1/lib/commons-logging.jar:
    /home/craft/whc/axis-1_1/lib/commons-discovery.jar:
    /home/craft/whc/axis-1_1/lib/jaxrpc.jar:
    /home/craft/whc/axis-1_1/lib/saaj.jar:." -Dhttp.proxySet=true -Dhttp.proxyHost="10.36.250.25" -Dhttp.proxyPort=8080 StockClient

    The result is follows:

    [craft:/home/craft/whc (465)] run_client.sh
    Result = <StockQuotes><Stock><Symbol>MSFT</Symbol><Last>23.81</Last><Date>8/5/2009</Date><Time>4:00pm</Time><Change>+0.04</Change><Open>23.93</Open><High>24.25</High><Low>23.79</Low><Volume>53310240</Volume><MktCap>212.2B</MktCap><PreviousClose>23.77</PreviousClose><PercentageChange>+0.17%</PercentageChange><AnnRange>14.87 - 28.50</AnnRange><Earns>1.619</Earns><P-E>14.68</P-E><Name>Microsoft Corpora</Name></Stock></StockQuotes>