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="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>com.howtodoinjava.jersey</groupId>
	<artifactId>JerseyDemos</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<repositories>
		<repository>
			<id>maven2-repository.java.net</id>
			<name>Java.net Repository for Maven</name>
			<url>http://download.java.net/maven/2/</url>
			<layout>default</layout>
		</repository>
	</repositories>
	<properties>
		<jersey2.version>2.19</jersey2.version>
		<jaxrs.version>2.0.1</jaxrs.version>
	</properties>
	<dependencies>
		<!-- JAX-RS -->
		<dependency>
			<groupId>javax.ws.rs</groupId>
			<artifactId>javax.ws.rs-api</artifactId>
			<version>${jaxrs.version}</version>
		</dependency>
		<!-- Jersey 2.19 -->
		<dependency>
			<groupId>org.glassfish.jersey.containers</groupId>
			<artifactId>jersey-container-servlet</artifactId>
			<version>${jersey2.version}</version>
		</dependency>
		<dependency>
			<groupId>org.glassfish.jersey.core</groupId>
			<artifactId>jersey-server</artifactId>
			<version>${jersey2.version}</version>
		</dependency>
		<dependency>
			<groupId>org.glassfish.jersey.core</groupId>
			<artifactId>jersey-client</artifactId>
			<version>${jersey2.version}</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>JerseyDemos</finalName>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

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.

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
 
<web-app>
 
  <display-name>Archetype Created Web Application</display-name>
   
  <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
             <param-name>jersey.config.server.provider.packages</param-name>
             <param-value>com.howtodoinjava.jersey</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
  
    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
     
</web-app>

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;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
 
@Path("/message")
public class JerseyService
{
    @GET
    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 (org.glassfish.jersey.media:jersey-media-sse:2.19) 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!

    Reply
  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.

    Khyati

    Reply
  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!!

    Reply
    • 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

      Reply
  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!

    Reply
  5. Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.apache.catalina.LifecycleException: java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map;.

    Reply
  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.

    Reply

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.