Click here to Skip to main content
11,925,849 members (62,732 online)
Click here to Skip to main content
Add your own
alternative version


33 bookmarked

Consuming Web Service in Java 5

, 3 Oct 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
The article describes how to consume a Web service in Java 5. (Creating proxy classes in Java was never so simple!)
Code working from Eclipse


This article describes how to consume Web services in Java 1.5.0 using the new JAX-WS 2.0 API (JSR 228).

Developers around the world, including me, have always complained about the hard ways to work in Java to consume even a Web service as simple as adding two numbers.

However, with JAX-WS 2.0 API now available in core Java in JDK 1.5.0, life is simple like never before.

The article describes how this API can be used for maximum benefits using some off-the-shelf tools similar to wsdl.exe available from Microsoft in the .Net Framework to generate the proxy classes.

Before starting with the discussion, some vocabulary background may be needed for first timers. Following are simple definitions to the terms used:

  • Web Service: An operation / task available on a remote application running in an unknown environment.
  • SOAP: A language in which the two unknown applications would talk to each other.
  • Message: The content transferred over wire using SOAP as the language.
  • WSDL: A language that would define the exact grammar of SOAP.
  • Proxy Class: A piece of code, available on the client machine, that would perform the task of making connections to the server and taking care of serialization and de-serialization.

Publishing a Web Service using ASP.Net

For our case, we would publish a Web service using ASP.NET. You can look into other articles around the globe onto how to publish a Web service using ASP.NET.

Here is the synopsis of the Web service:

Name: BasicWebServices
Operation: Add
Input Message: AddMessage
Output Message: AddMessageResponse
AddMessage: Two parameters, x and y, of type double
AddMessageResponse: One parameter, AddMessageResult, of type double

Consuming the Web Service using JAX-WS 2.0 in Java 1.5

Download JAX-WS Reference Implementation from here. Extract the files using the procedure described in the installation section of the document above.

Dig into the bin folder that would have a utility called wsimport. Yes! All those who have worked with wsdl.exe in the .NET Framework would immediately understand what the purpose of the tool is.

Apart from various other options, the important options supported by the tool are:

  • -p pkg: Name of the package where the source files will be generated
  • -s src-directory: Path to the directory where the source files will be kept
  • -d directory: Path to the directory where the binary files will be kept

On the console, issue the following command (all in one line):

    –s src
    –d bin

The last parameter is the location of the WSDL file. Yes, it works with HTTP transport beautifully.

Now… now, what? The proxy class is available. You will find the following classes generated:

  • BasicWebServices
  • BasicWebServicesSoap
  • AddMessage
  • AddMessageResponse
  • ObjectFactory

... and these are the proxy classes ready for use. See the code below for demonstration.

public class BasicWebServicesClient
    public static double testAdd(double x, double y)
        double retVal = 0;

        BasicWebServices service = new BasicWebServices();
        BasicWebServicesSoap soap = service.getBasicWebServicesSoap();

        ObjectFactory factory = new ObjectFactory();
        AddMessage request = factory.createAddMessage();

        AddMessageResponse response = soap.add(request);

        retVal = response.getAddMessageResult();

        return retVal;

And the main method to use the wrapper method testAdd described above:

public class MainClass
    public static void main(String[] args)
        double x = 34.56;
        double y = 45.67;

        double z = BasicWebServicesClient.testAdd(x, y);
        System.out.printf("%f + %f = %f", x, y, z);


Consuming Web services in Java requires just two steps now. The first step is to create proxy classes. The second step is to go ahead and use them.


  • 3rd October, 2006: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

India India

Gaurav lives in Bangalore, the Silicon Valley of India.

His technical strength lies in having deep understanding of not one or two but bunch of enterprise frameworks.

Lately, he is working on an MVC framework implementation for Android put in open domain at

Today, he is an independent guy:

  • He is a programming language independent guy, well almost. He is proficient in C, C++, Java, C#, VB.Net, C++.Net, JavaScript, PHP, Tcl, Python, Ruby
  • He is an OS independent guy, well almost. Has worked and developed at length on HP-UX, Linux (Redhat / Mandrake), Macintosh (9, 10.x), Windows (NT, 2000, 2003, XP, Vista), Solaris (8, 9, 10); and mobile platforms Android, iPhone, Blackberry
  • He is a target independent guy, well almost. Has worked on thick clients (mainly desktops) as well as thin clients (mainly alternative platforms - Symbian, PalmOS, WinCE, WinXP Embedded, Linux Embedded, Android, iPhone, Blackberry)

Today, his thrust areas are Service Oriented Architecture (implementation expert in Java, .Net and PHP; integration with Mainframe and other legacy environments), Mobile Technologies and RFID.

He holds a Bachelor's Degree (B. Tech.) from IIT Kanpur His major was in EE with specialization in DSP (SSP).

His hobby is listening music, reading books (no, he can't read novels), photography and travelling.

Should you wish to talk to him, you can drop him a mail at gaurav[dot]vaish[at]gmail[dot]com. He generally responds within 24-48 hrs unless there is a compelling reason.

And yeah... here's his personal website:

Smile | :)

You may also be interested in...

Comments and Discussions

QuestionJava-ws on Linux Pin
devvvy13-Jan-15 13:47
memberdevvvy13-Jan-15 13:47 
QuestionWeb service method implementation Pin
SivaRamChowdary3-Feb-13 9:21
memberSivaRamChowdary3-Feb-13 9:21 
GeneralMy vote of 4 Pin
Murthy Nulu26-Aug-12 22:34
memberMurthy Nulu26-Aug-12 22:34 
GeneralWebService call Pin
Anil Thapliyal31-Aug-10 6:55
memberAnil Thapliyal31-Aug-10 6:55 
QuestionDeploying and invoking the service Pin
vskale@yahoo.com3-Aug-10 22:32
membervskale@yahoo.com3-Aug-10 22:32 
Questionhave a problem in the generated code Pin
mouhannad5-Nov-09 10:46
membermouhannad5-Nov-09 10:46 
AnswerRe: have a problem in the generated code Pin
mastergaurav5-Nov-09 15:57
membermastergaurav5-Nov-09 15:57 
QuestionMay I call webservice(java) for Windown form! Pin
KanePro18-May-09 1:29
memberKanePro18-May-09 1:29 
AnswerRe: May I call webservice(java) for Windown form! Pin
mastergaurav5-Nov-09 15:56
membermastergaurav5-Nov-09 15:56 
GeneralDiffgrams --> java Pin
wouter devers5-Feb-07 1:43
memberwouter devers5-Feb-07 1:43 
GeneralRe: Diffgrams --> java Pin
mastergaurav5-Feb-07 17:48
membermastergaurav5-Feb-07 17:48 
GeneralRe: Diffgrams --> java Pin
wouter devers8-Feb-07 1:59
memberwouter devers8-Feb-07 1:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 3 Oct 2006
Article Copyright 2006 by mastergaurav
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid