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="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 https://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

Comments

Subscribe
Notify of
guest
15 Comments
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