jersey

Creating JAX-RS web service using Jersey Example

Introduction

Jersey as it states in the website, is more than just a reference implementation. It also has it’s own API that extends the specification toolkit with additional wrapped features and utilities to simplify RESTful service and client development. It also exposes numerous extension SPIs so that developers may extend Jersey to best suit their needs. For this post, we’re going to demonstrate how easy and simple it is to develop a RESTful service using this awesome technology!

What are we doing?

We’re doing a simple account details listing. So We will create a service that will display a json output of account details that can be parsed on the front-end.

1. Create a webapp using Maven and plug in the Jersey library

pom.xml

<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 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.javacodegeeks.areyes.jersey</groupId>
	<artifactId>jersey-example</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>jersey-example Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>javax.ws.rs</groupId>
			<artifactId>javax.ws.rs-api</artifactId>
			<version>2.0.1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-server</artifactId>
			<version>1.8</version>
		</dependency>

	</dependencies>
	<build>
		<finalName>jersey-example</finalName>
	</build>
</project>

2. Additional configuration

We also need to put additional configurations on the web.xml file as we need the app to recognize our service.

web.xml

<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>Restful Web Application</display-name>
 
	<servlet>
		<servlet-name>jersey-serlvet</servlet-name>
		<servlet-class>
                     com.sun.jersey.spi.container.servlet.ServletContainer
                </servlet-class>
		<init-param>
		     <param-name>com.sun.jersey.config.property.packages</param-name>
		     <param-value>com.javacodegeeks.jersey.main</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
 
	<servlet-mapping>
		<servlet-name>jersey-serlvet</servlet-name>
		<url-pattern>/rs/*</url-pattern>
	</servlet-mapping>
 
</web-app>

3. Create the Service

We now create the service. This service is located on the package we specified on the web.xml (com.sun.jersey.config.property.packages).

AccountDetailsService.java

package com.javacodegeeks.jersey.main;
 
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
 

@Path("account")
public class AccountDetailsService {
 
    @GET
    @Path("/details/{param}")
    @Produces(MediaType.TEXT_PLAIN)
    public Response getAccountDetails(@PathParam("param") String accountName) {
    	String output = "Account Name : " + accountName;
		return Response.status(200).entity(output).build();
    }
}

4. Let’s Test it!

Deploy your app to a J2EE container and go to:
http://localhost:8080/jersey-example/rs/account/details/

Figure 1: Jersey sample output
Figure 1: Jersey sample output

Download the Eclipse project of this tutorial:

Download
You can download the full source code of this example here : jersey-example

Alvin Reyes

Alvin has an Information Technology Degree from Mapua Institute of Technology. During his studies, he was already heavily involved in a number of small to large projects where he primarily contributes by doing programming, analysis design. After graduating, he continued to do side projects on Mobile, Desktop and Web Applications.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button