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

Web.xml

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

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

References

Jersey

Jackson

  • 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.

  • Subhendu

    package com.javahash.jersey;

    public class Welcome {

    private String domain;
    private String message;
    public String getDomain() {
    return domain;
    }
    public void setDomain(String domain) {
    this.domain = domain;
    }
    public String getMessage() {
    return message;
    }
    public void setMessage(String message) {
    this.message = message;
    }

    }