Developping and deploying webservice onto Tomcat

Deploying Webservices onto Tomcat is possible with the metro library.

Below is concise description on how to achieve that.

Configuration
Add to your web.xml the following servlet configuration

<listener>
    <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
    <servlet-name>service</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>service</servlet-name>
    <url-pattern>/MyService/*</url-pattern>
</servlet-mapping>

Here we add the metro listener on startup WSServletContextListener to initialize the Webservice context. We declare the Servlet (metro servlet) WSServlet and bind it to the context MyService (the context MyService name here is arbitrary).

To finalize configuration, create next to your web.xml (generally in the WEB-INF folder), the sun-jaxws.xml file with the following content

<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0">
    <endpoint name="service" implementation="com.ichir.projects.services.ServiceImpl" url-pattern="/MyService" />
</endpoint

As you can see, in this file, we bind the Webservice context (MyService) to the webservice implementation class com.ichir.projects.services.ServiceImpl.

The webservice implementation class (a simple Calculator/Summer) is a regular @Webservice annotated class

package com.ichir.projects.services;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public class ServiceImpl {
	public int calculate(@WebParam(name="a") int arg0, @WebParam(name="b") int arg1) {
		return arg0 + arg1;
	}
}

Deploy your webservice onto Tomcat and enjoy!

Downloads

References

Context

  • Maven 2 or higher
  • Servlet API 2.5
  • Metro library 2.2.x
Advertisements

One thought on “Developping and deploying webservice onto Tomcat”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s