Thursday, April 12, 2012

How to Publish, Consume a simple web service using Tomcat

Web services are platform independent hence it is used to communicate between different languages over http protocol. Web Services can convert your application into a Web-application, which can publish its function or message to the rest of the world. The basic Web Services platform is XML and HTTP.

Publishing and Consuming a Helloworld web service is very easy, all you need to have is a JDK (1.6 or higher),a Tomcat server and a IDE (e.g. Eclipse) to create a web service client you need to download stub. In this article we will see how to publish a simple JAX-WS web services using Tomcat servlet container. Also we will see how to create a simple web serive client. i.e. How to consume web service?

How to publish simple web service?

For running this example, below mentioned particulars are used.
  • Tomcat 7.0.11
  • JDK 1.6.0_21
  • MyEclipse 8.6
In this example, We have an interface HelloWS which contains a abstract method sayHello() which is to be exposed and a class which will implement HelloWS.

Please go through each of the screen shots.

Creating a New Web Service Project

Add project name, lets say SimpleWebservice

/* */

package com.javaxp;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface HelloWS {

public String sayHello(String name);

/* */

package com.javaxp;

import javax.jws.WebService;

public class HelloWSImpl implements HelloWS {

public String sayHello(String name) {

return "Hello "+name+"!! this is your first web service.";

web.xml (defines WSServletContextListener)

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"

sun-jaxws.xml (defines web service implementation class)

<?xml version="1.0" encoding="UTF-8"?>

If JAXB is not bundled with your tomcat you have to add below mentioned JAR files in ${Tomcat}/lib.
Download JAR file from

  • gmbal-api-only.jar
  • ha-api.jar
  • jaxb-impl.jar
  • jaxws-api.jar
  • jaxws-rt.jar
  • jaxb-api.jar
  • management-api.jar
  • policy.jar
  • stax-ex.jar
  • streambuffer.jar
  • jaxb-core.jar

Now deploy your web service project to tomcat and hit below mentioned URL, you should able to see below screen shot.



Below is the generated XML.

This XML file does not appear to have any style information associated with it. The document tree is shown below.
 Published by JAX-WS RI at RI's version is JAX-WS RI 2.2.6b21  svn-revision#12959.
 Generated by JAX-WS RI at RI's version is JAX-WS RI 2.2.6b21  svn-revision#12959.
<definitions targetNamespace="" name="HelloWSImplService">
<message name="sayHello">
<part name="arg0" type="xsd:string"/>
<message name="sayHelloResponse">
<part name="return" type="xsd:string"/>
<portType name="HelloWS">
<operation name="sayHello">
<input wsam:Action="" message="tns:sayHello"/>
<output wsam:Action="" message="tns:sayHelloResponse"/>
<binding name="HelloWSImplPortBinding" type="tns:HelloWS">
<soap:binding transport="" style="rpc"/>
<operation name="sayHello">
<soap:operation soapAction=""/>
<soap:body use="literal" namespace=""/>
<soap:body use="literal" namespace=""/>
<service name="HelloWSImplService">
<port name="HelloWSImplPort" binding="tns:HelloWSImplPortBinding">
<soap:address location="http://localhost:8080/SimpleWebservice/hellows"/>

How to consume a web service?

Consuming a web service is very easy all you need to have a WSDL url and an IDE i.e Eclipse for creating web service client. Provide WSDL url and download the stubs. Below mentioned files are automatically generated by WSDL.

Lets create a simple web service client.

Step by step process to create web service client. Please see the screen shots below.

Add project name, lets say SimpleWebserviceClient

Now creating a client.

Add WDSL URL http://localhost:8080/SimpleWebservice/hellows?wsdl

Now stubs are downloaded. You should able to see below mentioned classes. (exposed interface) (generated class)

Now we will creata

/* */

package com.javaxp;

public class TestClient {

public static void main(String[] args) {

HelloWSImplService service = new HelloWSImplService();

HelloWS helloWS = service.getHelloWSImplPort();