Jersey 2 hello world example – Jersey 2 tutorial

I have published lots of tutorials for developing REST apis using RESTEasy. In this Jersey 2 tutorial, I will go through configuration steps in detail for setting up a Jersey 2 example web application project.

Table of Contents

1. What changed from Jersey 1.x to Jersey 2.x
2. Jersey 2 maven dependencies
3. web.xml Changes
4. Jersey rest api code

1. What changed from Jersey 1.x to Jersey 2.x

Jersey team, which developed Jersey 1.x, joined new organization GlassFish and all new upgrades are released under since 2.x. It has changed a lot of things in framework functionalities. You can see list of changes in the official migration guide.

Though they have covered lots of changes in guide, but you may face lots of other changes and possibly will face hard time in getting solution for those. Keep in mind.

2. Jersey 2 maven dependencies

First change, you will need to make in your jersey 1.x application will be in pom.xml. Dependencies got changed. Use below dependencies for Jersey 2.x project. I am using jersey 2.19 (latest till date).

My pom.xml file looks like this.

<project xmlns="" xmlns:xsi=""
			<name> Repository for Maven</name>
		<!-- JAX-RS -->
		<!-- Jersey 2.19 -->

3. Jersey 2 example – web.xml changes

Second change you will need to do in web.xml file. Mostly changes are replacing old package names to new package names.

 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "" >
  <display-name>Archetype Created Web Application</display-name>

4. Jersey 2 example – REST APIs Code

REST service code will be mostly the same. If you find any problem, then simply refer to migration guide or drop me a comment.

package com.howtodoinjava.jersey;

public class JerseyService
    public String getMsg()
         return "Hello World !! - Jersey 2";

When you run above Jersey 2 application in tomcat 8 server and hit URL “http://localhost:8080/JerseyDemos/rest/message“, you will get below message.

jersey-2 hello world
jersey-2 hello world
If you find java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer exception in application start up, then find the solution in linked post.

Happy Learning !!

Reference: Jersey 2 User Guide

Was this post helpful?

Join 8000+ Awesome Developers, Like YOU!

15 thoughts on “Jersey 2 hello world example – Jersey 2 tutorial”

  1. Thank you very much for the example!

    I was looking to integrate a Jersey exposed resource with Server Side Events ( but couldn’t manage to have a working example. Any Jersey exposed resource stopped working the moment SSE dependency was mentioned in pom (it seems that version of SSE doesn’t work with Jersey < v2).

    Saved my day!

  2. HI,

    I copied the source code you gave and imported it as maven project in my Eclipse.
    The eclipse version I am using is Neon.2 Release (4.6.2).
    I then updated Project Facets and have added Dynamic Web Module 3.1 to the project.
    Post that I was able to add the code to Tomcat server and then when I run the above url I get 404 error.
    Also, I had to do one change in POM, i had to replace jersey2.version from 2.19 to 2.2 as within company we use our own repository and not maven one and we don’t have 2.19 version.
    Please help me.


  3. Great tutorial!! I tried so much tutorial on the web but none of them worked.
    This is the only one tutorial which worked well. I really appreciate this tutorial!!

    • First, run maven to create you war file.
      Then hot deploy war file to your tomcat server and open the browser to that given link above.
      The output will be Hello World !! – Jersey 2

  4. I followed a couple of online tutorials/articles step by step, none of them worked. This is only one, which works. Thank you so much!

  5. Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.apache.catalina.LifecycleException: java.lang.NoSuchMethodError:;.

  6. Thanks a lot! I tried a lot with tutorials for 1.x but with libraries of 2.x. I didn’t know that there were a lot of differences.


Leave a Comment

About HowToDoInJava

This blog provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.