How to write a simple RESTful web service using Jersey (Part 1)?

In this series, I’m going to show you how to write simple RESTful web services using the JAX-RS reference implementation named Jersey (and other implementations of JAX-RS, if time permits).

In part 1, we are going to see how to write a “Hello World” RESTful service that just returns “Hello World!!!” in plain text.

As a prerequisite, you need the below before executing it in your local PC.

  • A servlet container (like Tomcat) or a Java Application Server (like Glassfish) is up and running in your machine under port 8080.
  • Download following libraries from Jersey homepage  and copy under /WEB-INF/lib


Step 1: Add the below lines in web.xml

Description: By adding the below lines, Jersey ServletContains scans for implementation of Jersey Application classes which is shown in step 2.

	<servlet-class> com.glassfish.jersey.servlet.ServletContainer </servlet-class>
	<!-- load an instance of the Jersey ServletContainer during the WAR bootstrap process; the critical role of the
	ServletContainer is to scan the deployed WAR file for Jersey Application classes. -->

Step 2: Implement the Jersey Application class

Description: Method getClasses() iterates through the list of resource classes found. In our case, there is only one such class called “HelloWorldResources”.

package webservices;

import java.util.HashSet;
import java.util.Set;

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

Step 3: Implement the class that serves the resources

Description: @Path annotation at class level says that all the resources in this class is accessed by prefixing the path “/helloworldresources”.

The method getPlain() serves a resource by the name “/plain” in data format “text/plain” via HTTP GET request.

package webservices;


public class HelloWorldResources {
    public String getPlain() {
        return "Hello World!!!";

Step 4: Create WAR file and deploy

Create WAR file (named PlayTest-1.0.war) and deploy in the server. Now enter the below URL in your browser, and voila, “Hello World!!!” is displayed in plain text.


(Source code for the above can be found in


2 thoughts on “How to write a simple RESTful web service using Jersey (Part 1)?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s