Jersey Hello World Example

7 Flares Twitter 0 Facebook 1 Google+ 6 Pin It Share 0 LinkedIn 0 Filament.io 7 Flares ×

Jersey is a reference implementation of  JAX-RS( JSR 311) specification. It is used to develop RESTful web services. This tutorial shows you how to develop a Hello World applications using Jersey.

Technologies used

  1. Jersey 2.5.1
  2. JDK 1.6
  3. Tomcat 7
  4. Maven 3.x
  5. Eclipse Juno

Create Project

First we need to create a maven project. This can be done using the Eclipse IDE or manually from the command prompt. Here is how you can create the maven project via the command line

mvn archetype:generate -DgroupId=com.javahash.jersey-DartifactId=JerseyHelloWorld -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

To create the project using the IDE, refer to an earlier tutorial on Spring MVC  that has a section showing the creation of maven web project using Eclipse IDE.

Jersey Project Structure

Dependencies

Here is the complete pom.xml showing the dependencies

<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.javahash.jersey</groupId>
 <artifactId>JerseyHelloWorld</artifactId>
 <packaging>war</packaging>
 <version>0.0.1-SNAPSHOT</version>
 <name>JerseyHelloWorld 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>org.glassfish.jersey.containers</groupId>
 <!-- if your container implements Servlet API older than 3.0, use "jersey-container-servlet-core" -->
 <artifactId>jersey-container-servlet</artifactId>
 <version>2.5.1</version>
</dependency>
<!-- Required only when you are using JAX-RS Client -->
<dependency>
 <groupId>org.glassfish.jersey.core</groupId>
 <artifactId>jersey-client</artifactId>
 <version>2.5.1</version>
</dependency>
<dependency>
 <groupId>com.fasterxml.jackson.jaxrs</groupId>
 <artifactId>jackson-jaxrs-json-provider</artifactId>
 <version>2.2.3</version>
</dependency>
 </dependencies>
 <build>
 <finalName>JerseyHelloWorld</finalName>
 </build>
</project>

Web.xml

<!DOCTYPE web-app PUBLIC

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
 <display-name>Archetype Created Web Application</display-name>
 <servlet>
 <servlet-name>Jersey Web Application</servlet-name>
 <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
 <init-param>
 <param-name>jersey.config.server.provider.packages</param-name>
 <param-value>com.javahash.jersey</param-value>
 </init-param>
 <init-param>
 <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
 <param-value>true</param-value>
 </init-param>
 <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
 <servlet-name>Jersey Web Application</servlet-name>
 <url-pattern>/rest/*</url-pattern>
 </servlet-mapping>
 </web-app>

In web.xml, register “com.sun.jersey.spi.container.servlet.ServletContainer“, and put your Jersey service folder under “init-param“, “com.sun.jersey.config.property.packages“.

Declare “com.sun.jersey.api.json.POJOMappingFeature” as “init-param”. This will make Jersey support JSON/object mapping. Refer the above web.xml

Rest Service


package com.javahash.jersey;

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

/**
 * Root resource (exposed at "helloworld" path)
 */
@Path("helloworld")
public class HelloWorldREST {

/**
 * Method handling HTTP GET requests. The returned object will be sent to
 * the client as "text/plain" media type.
 *
 * @return String that will be returned as a text/plain response.
 */
 @GET
 @Produces(MediaType.TEXT_PLAIN)
 public String ping() {
 return "Hey, This is Jersey JAX-RS !";
 }
 @GET
 @Path("/hellojson")
 @Produces(MediaType.APPLICATION_JSON)
 public Welcome simplejson() {
 Welcome welcome=new Welcome();
 welcome.setDomain("www.javahash.com");
 welcome.setMessage("JSON Example");
 return welcome;
 }

}

Demo

http://localhost:8080/JerseyHelloWorld/rest/helloworld will produce a normal string response

JerseyHello

http://localhost:8080/JerseyHelloWorld/rest/helloworld/hellojson will produce the json reponse

{"message":"JSON Example","domain":"www.javahash.com"}

References

Jersey

Jackson

Share this post:

Recent Posts

  • Chris

    Great post. I’ve followed the instructions and everything works except the last step. I simply end up with “{}”

    I didn’t see the Welcome.java code posted. Could you post it please?

  • Danielson

    com.sun.jersey.api.json.POJOMappingFeature <- is not Jersey 2, see
    http://stackoverflow.com/questions/17568469/jersey-2-0-equivalent-to-pojomappingfeature

    ^^^^
    for POJO upgrade

  • reader

    example is incomplete, missing the Welcome class.