Jersey 2.x - Hello, world!

The first tutorial about Jersey: Jersey 1.x - Hello, world! shows the way to develop a simple RESTful Web service application using Jersey 1.19. This tutorial shows you the way to develop the same application using Jersey 2.20. Eclipse for Java EE developers and Apache Tomcat are used to develop the application. If you do not have them, please setup them before continuing this tutorial.

For Ubuntu users:
For any users:
Integrate Tomcat with Eclipse

Step 1:
Create a new “Dynamic Web Project” in Eclipse, by NewProjectDynamic Web Project.

Step 2:
Give the project name as “Jersey Demo” and click “Finish”, on the appeared dialog.

Step 3:
We need to create “web.xml” file in the WebContentWEB-INF folder. It will be created automatically, if the  Dynamic Web Module version is lower than 3.0; otherwise right click on the “Deployment Descriptor: Jersey Demo” and choose “Create Deployment Descriptor Stub”.

Step 4:
Using Maven, make library management easier than the pure Eclipse project. To convert this project to Maven project, right click on the project and select ConfigureConvert to Maven Project.

Step 5:
Provide the group id – “com.javahelps” and artifact id – “jerseydemo” in the appeared dialog, and click on the “Finish” button.

Step 6:
Add the following dependencies to the "pom.xml" file.


After modification, pom.xml should look like this:
<project xmlns="" xmlns:xsi=""



Step 7:
Expand the src folder in the Java Resources and create a new package "" inside it.

Step 8:
Create a new Java class “HelloWorldService” inside the package.

Step 9:
Modify the class as shown below.


public class HelloWorldService {

    public Response sayHello(@PathParam("name") String msg) {
        String output = "Hello, " + msg + "!";
        return Response.status(200).entity(output).build();

  • @Path("/sayhello") - The URL of this service is /sayhello.
  • @GET - This method is handling the Http method GET.
  • @Path("/{name}") - The URL for the specific method. In this case it can be any name, which will be passed as a parameter to the method.
Step 10:
Create another package inside the src folder and name it as "".

Step 11:
Create a new Java class "HelloWorldApplication" inside the new package and modify the class as shown below.

import org.glassfish.jersey.server.ResourceConfig;

public class HelloWorldApplication extends ResourceConfig {
    public HelloWorldApplication() {
        // Define the package which contains the service classes.

Jersey 1.x versions required to define the package which contains the Service classes in the web.xml file. According to Jersey 2.x, a subclass of ResourceConfig is used for that purpose.

Step 12:
Modify the web.xml which is available in WebContentWEB-INF as shown below:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""
    id="WebApp_ID" version="3.1">
    <display-name>Jersey Demo</display-name>

    <!-- Define ServletContainer of Jersey -->
        <!-- Define the ResourceConfig class -->

    <!-- Map all the URLs to the Jersey ServletContainer -->

Step 12:
Save all the changes and run the project.
To run the project, right click on the project and select Run AsRun on Server.

Step 13:
Enter a URL in the following format and check the output.

For example:

Find the project at Git Hub.
Next Post »


Write comments
Han Zheng
April 28, 2018 at 9:21 PM delete

Thanks! Very helpful


Contact Form


Email *

Message *