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 HelloWSImpl.java which will implement HelloWS.

Please go through each of the screen shots.

Creating a New Web Service Project


Add project name, lets say SimpleWebservice



/* HelloWS.java */

package com.javaxp;

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

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

public String sayHello(String name);
}

/* HelloWSImpl.java */

package com.javaxp;

import javax.jws.WebService;

@WebService(endpointInterface="com.javaxp.HelloWS")
public class HelloWSImpl implements HelloWS {

public String sayHello(String name) {

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

web.xml (defines WSServletContextListener)
${Tomcat}/webapps/SimpleWebservice/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
                xmlns="http://java.sun.com/xml/ns/javaee"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <listener>
        <listener-class>
                com.sun.xml.ws.transport.http.servlet.WSServletContextListener
        </listener-class>
    </listener>
</web-app>

sun-jaxws.xml (defines web service implementation class)
${Tomcat}/webapps/SimpleWebservice/WEB-INF/sun-jaxws.xml

<?xml version="1.0" encoding="UTF-8"?>
<endpoints
  xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
  version="2.0">
  <endpoint
      name="HelloWS"
      implementation="com.javaxp.HelloWSImpl"
      url-pattern="/hellows"/>
</endpoints>


If JAXB is not bundled with your tomcat you have to add below mentioned JAR files in ${Tomcat}/lib.
Download JAR file from https://jax-ws.java.net/

  • 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.

http://localhost:8080/SimpleWebservice/hellows


WSDL Url
http://localhost:8080/SimpleWebservice/hellows?wsdl


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 http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.6b21  svn-revision#12959.
-->
−
<!--
 Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.6b21  svn-revision#12959.
-->
−
<definitions targetNamespace="http://javaxp.com/" name="HelloWSImplService">
<types/>
−
<message name="sayHello">
<part name="arg0" type="xsd:string"/>
</message>
−
<message name="sayHelloResponse">
<part name="return" type="xsd:string"/>
</message>
−
<portType name="HelloWS">
−
<operation name="sayHello">
<input wsam:Action="http://javaxp.com/HelloWS/sayHelloRequest" message="tns:sayHello"/>
<output wsam:Action="http://javaxp.com/HelloWS/sayHelloResponse" message="tns:sayHelloResponse"/>
</operation>
</portType>
−
<binding name="HelloWSImplPortBinding" type="tns:HelloWS">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/>
−
<operation name="sayHello">
<soap:operation soapAction=""/>
−
<input>
<soap:body use="literal" namespace="http://javaxp.com/"/>
</input>
−
<output>
<soap:body use="literal" namespace="http://javaxp.com/"/>
</output>
</operation>
</binding>
−
<service name="HelloWSImplService">
−
<port name="HelloWSImplPort" binding="tns:HelloWSImplPortBinding">
<soap:address location="http://localhost:8080/SimpleWebservice/hellows"/>
</port>
</service>
</definitions>


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.

HelloWS.java (exposed interface)
HelloWSImplService.java (generated class)

Now we will creata TestClient.java

/* TestClient.java */

package com.javaxp;

public class TestClient {

public static void main(String[] args) {

HelloWSImplService service = new HelloWSImplService();

HelloWS helloWS = service.getHelloWSImplPort();
System.out.println(helloWS.sayHello("Madan"));
}

}

Output



1 comment:

  1. It is a nice tutorial.This is a unique concept i have ever seen. Thanks for this post.

    ReplyDelete