Jersey 2 hello world example – Jersey 2 tutorial

Lokesh Gupta

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


Notify of
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

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

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

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode