Saturday, April 21, 2012

Step by step guide to publish a simple Hello World web service using tomcat

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 file is used to defines WSServletContextListener

Path : ${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 file is used to defines web service implementation class

Path : ${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/
Folder Structure

  • gmbal-api-only.jar
  • jaxb-core.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


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>