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=""  





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



public class RestExample {

    public String sayHello(@PathParam("name") String name){
        StringBuilder stringBuilder = new StringBuilder("SandBox | Hello ");

        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="" xmlns:xsi=""  
         xsi:schemaLocation="" version="3.0">
    <display-name>Laszlo Gazsi - Sandbox</display-name>



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.