Monday, October 17, 2011

Manually Deploy JAX-WS Web Service on Tomcat - Perfect Step by Step Guid

Here’s a guide to show you how to deploy JAX-WS web services on Tomcat servlet container. See following summary steps of a web service deployment.
  1. Create a web service (of course).
  2. Create a sun-jaxws.xml, defines web service implementation class.
  3. Create a standard web.xml, defines WSServletContextListener, WSServlet and structure of a web project.
  4. Build tool to generate WAR file.
  5. Copy JAX-WS dependencies to “${Tomcat}/lib” folder.
  6. Copy WAR to “${Tomcat}/webapp” folder.
  7. Start It.
my java classes  for Web services are as under

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.server;

import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.ejb.Stateless;

/**
 *
 * @author Rehan.Abbas
 */
@WebService(serviceName = "SalesOrderWS")
@Stateless()
public class SalesOrderWS {


    /**
     * Web service operation
     */
    @WebMethod(operationName = "addSalesOrders")
    public String addSalesOrders(@WebParam(name = "salesOrders") com.server.SalesOrder[] salesOrders) {
        //TODO write your implementation code here:
        return null;
    }
   
   
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.server;
import java.util.*;
/**
 *
 * @author Rehan.Abbas
 */
public class SalesOrder {
    public int ID;
    public Date date;
    public int customerNumber;
    public int totalLines;
    public LineItem lineItem[];   
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.server;

/**
 *
 * @author Rehan.Abbas
 */
public class LineItem {

    public int serialNum;
    public int productID;
    public int quantity;   
}


2. sun-jaxws.xml

Create a web service deployment descriptor, which is also known as JAX-WS RI deployment descriptor – sun-jaxws.xml.
my
sun-jaxws.xml. is as

<?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.server.SalesOrderWS" name="SalesOrder" url-pattern="/SalesOrderWS"/>
</endpoints>

3. web.xml 

my web.xml is
<?xml version="1.0" encoding="UTF-8"?>

<web-app 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_3_0.xsd"
     version="3.0">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

4. context.xml ; c:\tomcat6\webapps\adder_ws\META-INF\context.xml
my is as under

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/SalesOrderWS"/>

5. JAX-WS Dependencies

By default, Tomcat does not comes with any JAX-WS dependencies, So, you have to include it manually.
1. Go here http://jax-ws.java.net/.
2. Download JAX-WS RI distribution.
3. Unzip it and copy following JAX-WS dependencies to Tomcat library folder “{$TOMCAT}/lib“.
  • jaxb-impl.jar
  • jaxws-api.jar
  • jaxws-rt.jar
  • gmbal-api-only.jar
  • management-api.jar
  • stax-ex.jar
  • streambuffer.jar
  • policy.jar

6. Deployment

Copy the generated WAR file to {$TOMCAT}/webapps/ folder and start the Tomcat server.
For testing, you can access this URL : http://localhost:8080/SalesOrder/SalesOrderWS?wsdl, if you see following page, it means web services are deploy successfully.

For further, a very good tutorial is at
http://stackoverflow.com/questions/2511547/how-to-manually-deploy-a-web-service-on-tomcat-6


 

No comments:

Post a Comment

Blog Archive