Jersey 2.x - Hello, world!

The first tutorial about Jersey: Jersey 1.x - Hello, world! shows the way to develop a simple RESTful Web service application using Jersey 1.19. This tutorial shows you the way to develop the same application using Jersey 2.20. Eclipse for Java EE developers and Apache Tomcat are used to develop the application. 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>org.glassfish.jersey.core</groupId>
  <artifactId>jersey-server</artifactId>
  <version>2.20</version>
 </dependency>

 <dependency>
  <groupId>org.glassfish.jersey.containers</groupId>
  <artifactId>jersey-container-servlet</artifactId>
  <version>2.20</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.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>2.20</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.20</version>
        </dependency>
    </dependencies>
</project>

Step 7:
Expand the src folder in the Java Resources and create a new package "com.javahelps.jerseydemo.services" inside it.

Step 8:
Create a new Java class “HelloWorldService” inside the package.

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

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 10:
Create another package inside the src folder and name it as "com.javahelps.jerseydemo.app".

Step 11:
Create a new Java class "HelloWorldApplication" inside the new package and modify the class as shown below.
package com.javahelps.jerseydemo.app;

import org.glassfish.jersey.server.ResourceConfig;

public class HelloWorldApplication extends ResourceConfig {
    public HelloWorldApplication() {
        // Define the package which contains the service classes.
        packages("com.javahelps.jerseydemo.services");
    }
}

Jersey 1.x versions required to define the package which contains the Service classes in the web.xml file. According to Jersey 2.x, a subclass of ResourceConfig is used for that purpose.

Step 12:
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"
    id="WebApp_ID" version="3.1">
    <display-name>Jersey Demo</display-name>

    <!-- Define ServletContainer of Jersey -->
    <servlet>
        <servlet-name>JerseyDemo</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <!-- Define the ResourceConfig class -->
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.javahelps.jerseydemo.app.HelloWorldApplication</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 *