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

‘javax.ws.rs:javax.ws.rs-api:2.0’
‘org.glassfish.jersey.containers:jersey-container-servlet:2.4’
‘org.glassfish.jersey.core:jersey-server:2.4’

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>
	<servlet-name>jersey</servlet-name>
	<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. -->
	<load-on-startup>1</load-on-startup>
</servlet>

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 javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;

@ApplicationPath("/helloworldapp")
public class HelloWorldApplication extends Application {
    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> set = new HashSet<>();
        set.add(HelloWorldResources.class);
        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;

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

@Path("/helloworldresources")
public class HelloWorldResources {
    @GET
    @Produces({MediaType.TEXT_PLAIN})
    @Path("/plain")
    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.

http://localhost:8080/PlayArea-1.0/helloworldapp/helloworldresources/plain

(Source code for the above can be found in https://github.com/fizalihsan/PlayArea/)

Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s