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 are 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 show on the webs site and is: http://www.webservicex.net/stockquote.asmx?WSDL
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.
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 enter the URI of the WSDL and move the slider to select Develop Client. Client Finish.
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.
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:
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:
<StockQuotes> <Stock> <Symbol>IBM</Symbol> <Last>194.93</Last> <Date>7/5/2013</Date> <Time>4:00pm</Time> <Change>+1.68</Change> <Open>192.83</Open> <High>195.16</High> <Low>192.35</Low> <Volume>2407971</Volume> <MktCap>216.1B</MktCap> <PreviousClose>193.25</PreviousClose> <PercentageChange>+0.87%</PercentageChange> <AnnRange>181.85 - 215.90</AnnRange> <Earns>14.501</Earns> <P-E>13.33</P-E> <Name>International Bus</Name> </Stock> </StockQuotes>
5. Quite a lot of information is returned to us and as its in XML format we have a lot of choices as how to now treat it. Here are a few suggestions:
- Use an XML parser to select the data we want and display it to the client in a JSP.
- 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.