You are on page 1of 3

MIAN ZESHAN FAROOQI

Home

Articles
Presentation at ICOSST/OSSW 2010
Embedded MySQL Part 1

Consuming Web Services in Java using JAX-WS


History:
I had been using Axis2 in my project to consume web services. Everything was going well but unfortunately one of
the services message conflicted with Axis2 and I started getting exception of Unexpected sub element. I modified
my services messages and tried everything that came to my mind but the exception was still there. After searching I
found that this issue was first time logged in 2008 and still many people are facing this issue in 2011. So it was the
time to move ahead and I found JAX-WS.

Installation:
JAX-WS works with Java Standard Edition (J2SE) 5 or later[1]. If you dont already have JAX-WS then follow the
following steps otherwise you can directly move to Creating service client section:
1.

Download it from http://jax-ws.java.net.

2.

Double click the jar or extract it some other way (lets say at C:\jax-ws)

3.

Now you need to set an environment variable JAXWS_HOME and give the root folder of JAX-WS as value of
this variable e.g. JAXWS_HOME = C:\jax-ws

4.

Add %JAXWS_HOME%\bin in your Path variable. (optional)

Example:
Lets consume the Currency Convertor web service provided by http://www.websericeX.net. You may find this
service at http://www.webservicex.net/ws/WSDetails.aspx?WSID=10&CATID=2.
This service defines an operation ConversionRate that take two parameters FromCurrency and ToCurrency and
returns a type double value. FromCurrency and ToCurrency are of type strings. In java code well pass two strings and
expect a double value from this service. Lets see how we can do this.

Creating Service Client:


Open command prompt and use wsimport command by passing it the URL of service you want to consume. In our
case well use it like this:
wsimport http://www.webservicex.net/CurrencyConvertor.asmx?WSDL -Xnocompile
-Xnocompile is used when you dont want to compile the generated java classes. You may also need to use
-Xendorsed parameter at the end of this command (if required).[2]
This command will create source files of the web service client in a package.
Import this package in your Java Project and enjoy yourself!

Using created service client:


I am currently using eclipse but you can use any other IDE of your choice.
1.

Create a java project

2.

Import the generated source code in this project (net.webservicex.*)

3.

Create a Driver class (any java class that will execute the code)

4.

Now simply create objects of genereated service classes and assemble them togather to use the service.
The questions like Which objects are to be created and How to assemble them can be answered by reading
the WSDL file through which we have created the source code.

5.

For this particular example (and also for general guideline) please see the following piece of code that will
use the service:

import net.webservicex.*;

2
3
4
5

public class Driver {


public static void main(String[] arg){

// Create an instance of service stub

CurrencyConvertor service = new CurrencyConvertor();

8
9

// Create an instance of SOAP message specific to this service

10

CurrencyConvertorSoap msg = service.getCurrencyConvertorSoap();

11
12

// Invoke the method

13

double rate = msg.conversionRate(Currency.USD, Currency.PKR);

14
15

// Any java code to manipulate the reuslts

16

System.out.println("USD to PKR rate = " + rate);


}

17
18

[1] Ive tested it on J2SE 6


[2] Type wsimport and press enter to see the detailed help
http://zeshanfarooqi.wordpress.com/2011/04/29/consuming-web-services-in-java-using-jax-ws/

You might also like