Technical blog

March 9, 2010

Running JAX-WS with Tomcat

Filed under: java — Tags: , — paawak @ 00:18

JAX-WS is not supported out of the box. But its very easy to make it run on Tomcat or any other web server for that matter.

I would recommend Netbeans IDE for starters, as its very easy to quickly code and run a webservice.

This is how the implementation looks like. Note that its a POJO with a few annotations.

package com.swayam.webservice;
 
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
 
/**
 *
 * @author paawak
 */
@WebService()
public class UserManager {
 
    /**
     * Web service operation
     */
    @WebMethod(operationName = "addUser")
    public boolean addUser(@WebParam(name = "userName")
    String userName) {
        System.out.println(userName);
        return true;
    }
 
}

In addition to this, you also have to edit the WEB-INF/web.xml as follows:

<?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>
    <servlet>
        <servlet-name>jax-ws</servlet-name>
        <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
        </servlet>
    <servlet-mapping>
        <servlet-name>jax-ws</servlet-name>
        <url-pattern>/UserManager</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    </web-app>

Also, you have to tell the JAX-WS runtime about the endpoint or the implementation. This is done by having a non-standard file called WEB-INF/sun-jaxws.xml. The entry is:

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

Once this is done, download JAX-WS from here. A small word of warning. Version 2.2 is buggy, so its advisable to use an older one. I used 2.1.7 without any issues. Put all the jars from the lib folder into the WEB-INF/lib folder of your webapp. Now you are ready to go. Deploy it on Tomcat, it should work like breeze.

You will find the sources here. This is a Netbeans project. Also, you can try the war file directly.

2 Comments »

  1. very useful post…. saved a lot of time for me….

    Comment by Dyutiman — September 2, 2010 @ 19:38

  2. [...] There is one more configuration in Tomcat we have to do to make the web service working. Details of that can be found at Running JAX-WS with Tomcat [...]

    Pingback by Web Service for Lucene Search on Tomcat « The Other Side — October 1, 2010 @ 14:21

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress