Running JAX-WS with Tomcat

Posted by {"name"=>"Palash Ray", "email"=>"paawak@gmail.com", "url"=>"https://www.linkedin.com/in/palash-ray/"} on March 08, 2010 · 2 mins read

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:



    
        com.sun.xml.ws.transport.http.servlet.WSServletContextListener
    
    
        jax-ws
        com.sun.xml.ws.transport.http.servlet.WSServlet
        1
        
    
        jax-ws
        /UserManager
    
    
        
            30
        
    
    
        index.jsp
        
    

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:



  

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 https://github.com/paawak/blog/tree/master/code/WebServiceExample. This is a Netbeans project. Also, you can try the war file directly.