Jersey 1.x - Hello, world!

Developing RESTful Web services that seamlessly support exposing your data in a variety of representation media types and abstract away the low-level details of the client-server communication is not an easy task without a good toolkit. In order to simplify development of RESTful Web services and their clients in Java, a standard and portable JAX-RS API has been designed. Jersey RESTful Web Services framework is open source, production quality, framework for developing RESTful Web Services in Java that provides support for JAX-RS APIs and serves as a JAX-RS (JSR 311 & JSR 339) Reference Implementation.

This tutorial shows how to develop a simple hello world RESTful application using Jersey 1.19. This tutorial uses Eclipse for Java EE developers and Apache Tomcat as the development tools. If you do not have them, please setup them before continuing this tutorial.

For Ubuntu users:
For any users:
Integrate Tomcat with Eclipse

Step 1:
Create a new “Dynamic Web Project” in Eclipse, by NewProjectDynamic Web Project.



Step 2:
Give the project name as “Jersey Demo” and click “Finish”, on the appeared dialog.


Step 3:
We need to create “web.xml” file in the WebContentWEB-INF folder. It will be created automatically, if the  Dynamic Web Module version is lower than 3.0; otherwise right click on the “Deployment Descriptor: Jersey Demo” and choose “Create Deployment Descriptor Stub”.


Step 4:
Using Maven, make library management easier than the pure Eclipse project. To convert this project to Maven project, right click on the project and select ConfigureConvert to Maven Project.


Step 5:
Provide the group id – “com.javahelps” and artifact id – “jerseydemo” in the appeared dialog, and click on the “Finish” button.


Step 6:
Add the following dependencies to the "pom.xml" file.
<dependencies>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.19</version>
    </dependency>

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-servlet</artifactId>
        <version>1.19</version>
    </dependency>
</dependencies>

After modification, pom.xml should look like this:
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.javahelps</groupId>
    <artifactId>jerseydemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>1.19</version>
        </dependency>

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-servlet</artifactId>
            <version>1.19</version>
        </dependency>
    </dependencies>
</project>

Step 7:
Expand the src folder in the “Java Resources” and create a new package inside it.


Step 8:
Give the package name as “com.javahelps.jerseydemo


Step 9:
Right click on the package and select NewClass to create a new Java class “HelloWorldService”.


Step 10:
Modify the class as shown below.
package com.javahelps.jerseydemo;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("/sayhello")
public class HelloWorldService {

    @GET
    @Path("/{name}")
    public Response sayHello(@PathParam("name") String msg) {
        String output = "Hello, " + msg + "!";
        return Response.status(200).entity(output).build();
    }

}
  • @Path("/sayhello") - The URL of this service is /sayhello.
  • @GET - This method is handling the Http method GET.
  • @Path("/{name}") - The URL for the specific method. In this case it can be any name, which will be passed as a parameter to the method.

Step 11:
Modify the web.xml which is available in WebContentWEB-INF as shown below:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">
    <display-name>Jersey Demo</display-name>
    <servlet>
        <servlet-name>JerseyDemo</servlet-name>
        <!-- Define ServletContainer of Jersey -->
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <!-- Define the package to search for classes -->
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.javahelps.jerseydemo</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map all the URLs to the Jersey ServletContainer -->
    <servlet-mapping>
        <servlet-name>JerseyDemo</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

Step 12:
Save all the changes and run the project.
To run the project, right click on the project and select Run AsRun on Server.

Step 13:
Enter a URL in the following format and check the output.
../jerseydemo/sayhello/<Your-Name-Here>

For example:
../jerseydemo/sayhello/Gobinath


Find the project at Git Hub.
Previous
Next Post »

Contact Form

Name

Email *

Message *