Jersey Hello World

last modified July 13, 2020

Jersey Hello World tutorial shows how to create a simple Hello World RESTful Java web application with Jersey framework.


Java API for RESTful Web Services (JAX-RS) is a Java API specification that provides support in creating web services according to the Representational State Transfer (REST) architectural pattern. JAX-RS uses annotations to simplify the development and deployment of web service clients and endpoints.


Jersey is a framework for developing RESTful Web Services in Java. It is a reference implementation of the Java API for RESTful Web Services (JAX-RS) specification.

Jersey Hello World example

The following example is a simple RESTful application, which returns a plain text message to the client.

$ tree
├── nb-configuration.xml
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── zetcode
    │   │           ├── conf
    │   │           │   └── ApplicationConfig.java
    │   │           └── ws
    │   │               └── HelloResource.java
    │   └── webapp
    │       └── META-INF
    │           └── context.xml
    └── test
        └── java

This is the project structure.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 







This is the Maven POM file. It contains the jersey-container-servlet and jersey-server dependencies.

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/JerseyHelloWorld"/>

In the Tomcat's context.xml configuration file, we define the application context path.

package com.zetcode.conf;

import com.zetcode.ws.HelloResource;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

public class ApplicationConfig extends Application {

    public Set<Class<?>> getClasses() {
        Set<Class<?>> set = new HashSet<>();
        return set;

This is the application configuration class. Since Servlet 3.0 it is possible to deploy application without the web.xml file. In Jersey, we create a configuration class that extends the abstract Application and use the @ApplicationPath annotation. The Application defines the components of a JAX-RS application and supplies additional meta-data. Here we register resource classes, providers, or properties the application needs.


With the @ApplicationPath annotation, we set the path to RESTful web services.

public Set<Class<?>> getClasses() {
    Set<Class<?>> set = new HashSet<>();
    return set;

Inside the getClasses() method, we add the resource classes. In our case, we have one HelloResource class.

package com.zetcode.ws;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

public class HelloResource {

    public Response hello() {

        String output = "Hello World!";

        return Response.status(200).entity(output).build();

This is the resource class.

public class HelloResource {

The @Path specifies the URL to which the resource responds.


The @GET annotation indicates that the method responds to HTTP GET requests. The @Produces annotation defines the media type(s) that the method of a resource class can produce. In our case, the hello() method returns plain text.

return Response.status(200).entity(output).build();

We build a response using fluent style API.

$ curl localhost:8084/JerseyHelloWorld/rest/hello
Hello World!

After the application is deployed to Tomcat, we send a GET request to the application with curl.

In this tutorial, we have created a simple Hello World application in Jersey.

You might also be interested in the following related tutorials: Java tutorial, JAX-RS @PathParam tutorial, Spring Boot Jersey tutorial, Jersey application with embedded Jetty, and Web URL in a Jersey application.