Developers, Developers, Developers! Maksim Sorokin IT Blog

20Jan/11Off

JAX-WS web services + Maven + Tomcat

There is an awesome article about Deploy JAX-WS web services on Tomcat. It describes how to have a web service running without much effort.
We will follow the article and do the same, but with three differences:

  • We will build it with Maven
  • We will omit interface in the web service
  • We will not copy jars to the Tomcat. All dependencies will be managed by Maven

Let's start by creating a fresh maven project jaxwsExample. pom.xml will contain single JAX-WS dependency (which will be automatically downloaded from Maven Central) and will look as following:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>dk.sorokin.maksim</groupId>
  <artifactId>jaxwsExample</artifactId>
  <packaging>war</packaging>
  <version>1.0.0-SNAPSHOT</version>

  <name>JAX-WS Example</name>

  <dependencies>
    <dependency>
      <groupId>com.sun.xml.ws</groupId>
      <artifactId>jaxws-rt</artifactId>
      <version>2.1.3</version>
    </dependency>
  </dependencies>
</project>

The project was war type, so there fore it has to look as described in War plugin page.

We create a package inside src/main/java with the name jaxwsExample.ws and create class HelloWs with web service annotations as in mkyong's article, but without interface. We just put all annotations inside the class itself:

package jaxwsExample.ws;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService
@SOAPBinding(style = Style.RPC)
public class HelloWs {

  public String sayHello(String name) {
    if (name == null) {
      return "Hello";
    }

    return "Hello, " + name + "!";
  }
}

Then we create sun-jaxws.xml file and put it to src/main/webapp/WEB-INF folder:

<?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="jaxwsExample.ws.HelloWs" url-pattern="/HelloWs" />
</endpoints>

After it, we create web.xml in the same directory src/main/webapp/WEB-INF folder, again following original article:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  id="WebApp_ID" version="2.5">

  <display-name>jaxwsExample</display-name>

  <listener>
    <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>helloWs</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>helloWs</servlet-name>
    <url-pattern>/HelloWs</url-pattern>
  </servlet-mapping>
  <session-config>
    <session-timeout>120</session-timeout>
  </session-config>
</web-app>

And that is it. We do not need to define compilation manually, as it is done by maven. We also do not need to drop JAX-WS dependencies to Tomcat lib folder as they are managed by maven.

Now we just build the project with maven by mvn clean install, rename it to jaxwsExample.war (we can do that automatically in maven, but omit it for simplicity) and deploy it to the Tomcat.

Now we can access the web service on http://localhost:8080/jaxwsExample/HelloWs.

If you take a look inside the war file, you will notice that it added other dependencies automatically.

You can download the sources of jaxwsExample

Comments (1) Trackbacks (1)
  1. Thanks! You saved my life!


Leave a comment