HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Jersey / Jersey 2 Hello World

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.
Sourcecode download

Happy Learning !!

Reference: Jersey 2 User Guide

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Iulian

    December 16, 2017

    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!

  2. Hamid

    May 26, 2017

    Nice work,
    Thanks

  3. Khyati

    May 9, 2017

    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

    • Sandeep Kale

      May 12, 2019

      Same error am getting. Did you find some solution for it ?

  4. Tsubasa Narita

    January 22, 2017

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

  5. Rina

    December 12, 2016

    why http://localhost:8080/JerseyDemos/rest/message fall HTTP Status 404 – /JerseyDemos/rest/message

    type Status report? Please help me…

    • happy

      November 3, 2019

      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

  6. Alik Elzin

    November 21, 2016

    What should the folder structure be?

  7. Tony

    August 25, 2016

    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!

  8. Adam Darski

    August 22, 2016

    Hey bro, thank you so much for this, this works like a charm! excellent tutorial.

  9. rakesh KUMAR

    February 24, 2016

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

    • Lokesh Gupta

      February 24, 2016

      Try this: https://stackoverflow.com/questions/28509370/nosuchmethoderror-on-startup-in-java-jersey-app

      • rakesh KUMAR

        February 26, 2016

        I find ” java.lang.ArrayIndexOutOfBoundsException: 3 ” during build uploads time.

  10. Ioan Vraau

    February 10, 2016

    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.

  11. SEBASTIAN NAVA

    November 21, 2015

    NICE WORK :3

Comments are closed on this article!

Search Tutorials

Jersey Tutorial

  • Jersey – Hello World
  • Jersey2 – Hello World
  • Jersey – quickstart-archetype
  • Jersey – Custom Logging
  • Jersey – Set Cookie
  • Jersey – File Download
  • Jersey – File Upload
  • Jersey – Multi-File Upload
  • Jersey – Exception Handling
  • Jersey – MOXy JSON
  • Jersey – JSONP
  • Jersey – Google Gson
  • Jersey – Security

Jersey Client

  • Jersey Client – Access REST APIs
  • Jersey Client – Authentication
  • Jersey Client – Set Cookie

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces