Sunday 28 April 2013

Generate a Web Service Client from a WSDL using Jdeveloper


I recently had to code one half of an automated service which would connect to a Webservice and retrieve data based on the parameters sent. It needed to be converted back to XML from a Java Object and stored in a local directory. It wasn’t an easy one to start with as it involved security/authentication certificates which although I have had exposure to with OID/Single Sign on and external Tiers in the E-Business Suite, I had never really got to grips with, after this little project I sure have.

The other half which I didn’t have to code consisted of java stored procedures in an Oracle database which loaded the XML file , validated it and then loaded it into some target tables. This was all to remove the manual process of downloading and uploading the file which needed down by an end user daily. I’ve played around with this on my own time and will expand on this tutorial of sorts in the future to cover that.
First thing is first, download the latest version of Jdeveloper, I’ve used 11g Release 2

Finding an example to use isn’t that hard, there are plenty of websites which list publicly available webservices.

http://www.xmethods.net
http://www.webservicex.net/ws/default.aspx

I’ve decided to use the stock quote example , the wsdl can be found at the below
http://www.restfulwebservices.net/wcf/StockQuoteService.svc?wsdl

That’s enough links for now !!!
Open up Jdeveloper





Choose New Application > Custom Application
Enter an Application Name


Select Next
Enter Project Name

Click Finish
Right Click the newly created project and select New.

Under Categories, ‘Select Web Services’
Under Items, ‘Web Service Client and Proxy’

 
 
For this it was left as default. JAX-WS Style


Select Next
It is here you paste in the address of the WSDL.

Copy WSDL into Project.
 
Click Next to go through the other options but for me I choose to Finish.
Jdeveloper will start importing in the required packages and classes and building the client.



You should now have something like the above in your project.
The basic code for the client is ready


Thanks to my excellent naming it looks like I’ve ended up with quite the mouthful for the client file , oh dear, never mind.
StockQuoteService defines a Java interface that represents the service. It defines a method that returns a stub for the service.
IStockQuoteService defines a Java interface that represents the methods of the Web Service. These methods will match up with the elements in the WSDL

The client as its created first creates an instance of the StockQuoteService class which implements the interface which represents the service. It then retrieves a stub that represents the remote Web Service.

This is all well and good but we haven’t actually interacted with the webservice yet to get back anything meaningful.

To do this you should use the below code
Create JAXB object instance to accept the response

      StockQuote stockQuote = new StockQuote();
Populate the JAXB instance by calling the interface to the Webservice with a value, in this case  the ticker symbol for Oracle
      stockQuote = iStockQuoteService.getStockQuote("ORCL");
What you then do with the object is very much open, you can take individual elements and print them to a text file after converting them to a string. I choose to output the whole object as an XML file by using the JAXB Marshaller class which is used to convert java objects to XML 
I added the below to my imports for the class BasicHttpBinding_IStockQuoteServiceClient

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

I added this entry to the StockQuote.java above the class definition to allow the Marsharller to have a root reference

@XmlRootElement(name="StockQuote")

The last part was to write the code to convert the object and output to the console, you can see below the code and result of running this.

This was just a very basic attempt at interacting with a webservice that I thought I’d write up, hopefully it gets some people started and over a few bumps when using jdeveloper.

 

 

No comments:

Post a Comment