Press "Enter" to skip to content

RESTful Web Service example on Tomcat using Jersey

Previously I have published a small example of a RESTful Web Service in Java using RESTEasy and JBoss. Today I have created another example, which uses Jersey and may be deployed on Tomcat…

Jersey is the implementation of the JAX-RS from Oracle, so if I want to deploy a REST services on a simple web server, such as Tomcat, which does not come with any implementation of the JAX-RS, this is what I personally would use.

First of all we need to list our dependecies in the pom.xml. We need to add both com.sun.jersey.jersey-server and com.sun.jersey.jersey-servlet, since jersey-servlet contains the com.sun.jersey.spi.container.servlet.ServletContainer class needed to set up the rest servlet later in the web.xml

<?xml version="1.0" encoding="UTF-8"?>  
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>net.gazsi.laszlo.sandbox</groupId>
    <artifactId>tomcat-jersey-example</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>jsr311-api</artifactId>
            <version>1.1.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>1.18.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-servlet</artifactId>
            <version>1.18.1</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>sandbox</finalName>
    </build>


</project>  

I am using the same simple WS method I used in the previous example with RESTEasy.

package net.gazsi.laszlo.sandbox.ws;

import javax.ws.rs.GET;  
import javax.ws.rs.Path;  
import javax.ws.rs.PathParam;

@Path("hello")
public class RestExample {

    @GET
    @Path("{name}")
    public String sayHello(@PathParam("name") String name){
        StringBuilder stringBuilder = new StringBuilder("SandBox | Hello ");
        stringBuilder.append(name).append("!");

        return stringBuilder.toString();
    }

}

Setting up the web.xml when using Jersey seems a bit less elegant to me compared to RESTEasy, but it is not that complicated. We simply register a ServletContainer and define our packages that contain REST web services in the init parameters.

<?xml version="1.0" encoding="UTF-8"?>  
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_3_0.xsd" version="3.0">
    <display-name>Laszlo Gazsi - Sandbox</display-name>

    <servlet>
        <servlet-name>rest-ws</servlet-name>
        <servlet-class>
            com.sun.jersey.spi.container.servlet.ServletContainer
        </servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>net.gazsi.laszlo.sandbox.ws</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>rest-ws</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

</web-app>  

I have deployed the example on Tomcat 8 and when I made a GET request to http://localhost:8080/sandbox/rest/hello/Laszlo, it yielded SandBox | Hello Laszlo!, just as it should have.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *