News Ticker

Step 1: Web Service Client

This article walks you through the creation of a web services client from a URL pointer. We are going to use a location service WSDL to create the client in eclipse Juno. I will use a free web service that returns a stock quote for a given stock code. There is a range of free web services that can be used for this demo on the WebservicesX.NET web site.

The WSDL service locator for the stock quote web service is shown on the webs site and is:

To start we create a Dynamic Web Project in eclipse. I want to extend this project later by adding a servlet and HTML5 with AJAX to show the stock quote so I start with a project that is based on a typical web project.

1. First, we create the project. Go to new and select Dynamic Web Project and enter the name of the project. I will call this project WebServicesDemo. Click Finish to create the project.

Create Dynamic Web Project

Create Dynamic Web Project

2. Now we create the Web Services Client. Click right on the WebServicesDemo and select Web Services Client from the Web Services menu. The following dialogue box will appear. In the services, definition enters the URI of the WSDL and move the slider to select Develop Client. Client Finish.

Create Web Services Client

Create Web Services Client

3. A package called NET.webserviceX.www will be created in the Java Resources directory. These classes are responsible for calling the web service and processing the response.

Generated Classes

Generated Classes

4. Now that the web service client is created we now need to call the service and pass it a stock code so that it can retrieve a stock quote. We do this as follows:

new StockQuoteLocator().getStockQuoteSoap().getQuote("IBM");

The String that is returned is an XML representation of the stock quote information for the company with stock code IBM and looks like this:

<AnnRange>181.85 - 215.90</AnnRange>
<Name>International Bus</Name>

5. Quite a lot of information is returned to us and as it’s in XML format we have a lot of choices as for how to now treat it. Here are a few suggestions:

  1. Use an XML parser to select the data we want and display it to the client in a JSP.
  2. Send the XML string to a web page and let JavaScript process it.
  3. Marshal the XML using JAXB to a StockQuote object and send it to a JSP to be displayed to the client.

6. I think that it would be very interesting to marshal the XML to an object. This gives us a lot of further choices about what to do with the stock quote data.

The next step is to marshal the XML to a StockQuote object using JAXB.


Leave a Reply