JAX WS Tutorial with Example code

31 Flares Twitter 0 Facebook 8 Google+ 17 Pin It Share 0 LinkedIn 6 Filament.io 31 Flares ×


Starting with JDK 1.6,  JAX-WS is available with Java Installation. Hence it is now easier to develop web services in Java. No need to separately download other libraries. All you need is in your JDK installation. This tutorial will explore JAX – WS in detail. We will see how we can create  SOAP RPC Style WebServices using JAX – WS

In Official terms a web service means – method of communication between two electronic devices over a network. It is a software function provided at a network address over the web with the service always on as in the concept of utility computing.

Let us create a Hello World RPC style SOAP Web service

1. Define the Web Service Endpoint

2. Create Web Service Implementation

3. Publish the Service

We need to publish the service for others to know about it and be able to consume it. We use an Http end point to make the service available to the consumers.

4. The WSDL

Details about the service can be accessed using the dynamically generated WSDL. You just need to append  ?wsdl to the endpoint url to retreive the WSDL.



In the above WSDL file, you can find a targetNamespace. This is the namespace where our service is defined and this along with the service name will be required for the clients to consume the service.

Java Client

Let us write a  Java Client to consume the above service. It is pretty straight forward to write the client if there are no complex types in the request and response. In our case, it is just a String both ways. Due to this, we can write the client straightway with out using any utilities.

But the real world is different and we have to depend on complex types to pass information around. Writing a client in such a scenario is tedious and so we have to use some tools to ease our job. The tool available in JDK , to do this is the wsimport tool. This tool will automatically generate all the required client files by parsing the wsdl file.

Java client using  wsimport


 wsimport WSDL

In our case it will be

wsimport http://localhost:9004/webservice/sayhello?wsdl

The above command will generate the class files (required for the client ) in a package inside the folder from where you issue the wsimport command. It will only have the class files. If you need to see what is inside those files, you can use the -keep option to keep the java source files.

wsimport  -keep http://localhost:9004/webservice/sayhello?wsdl

Once the required client files are generated we can write a simple java main program utilizing the generated classes to consume the web service.

Hope this tutorial is useful to you. If you like it and found useful, do share your appreciation by sharing the tutorial in social media.

  • One missing piece is – how to see the actual SOAP Request and Response.
    Anyone know how to log or view the SOAP Request / Response while consuming service using java client.

  • Devender

    This site is really helpful, I have one doubt, How can I generate client files using command prompt, you provide the commands but not working or not clear so please provide clear information about generate client files using command prompt.
    Thank you.

  • ramu

    need job search in http://www.biddinglab.com