Creating RESTful webservice with Jersey

In this article, we will create a restful webservice using Jersey framework.

 

Here are the tools & technologies used :

  • Eclipse
  • Jersey framework
  • Maven
  • Tomcat server

 

Here are the steps for this :

 

Step 1 (Using maven, create the project structure)

 

Run the following maven archetype command :

 

mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-webapp -DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false -DgroupId=com.topjavatutorial -DartifactId=jersey-webapp -Dpackage=com.topjavatutorial -DarchetypeVersion=2.22.1

 
jersey maven archetype
 

Step 2 (Import project in Eclipse)

 

Now open the project in Eclipse by selecting File -> Import -> Existing Maven projects

 

You may refer the following article for the same :

 

Import Maven project in Eclipse

 

jersey project structure
 

Project sources are located under src/main/java.

Project web application files are located under src/main/webapp.

 

Project sources contains com.topjavatutorial package. This package contains MyResource class, that contains implementation of a simple JAX-RS resource.

 

Modify the class as shown below:

 

package com.topjavatutorial;

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 "myresource" path)
 */
@Path("myresource")
public class MyResource {

    /**
     * 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 hello() {
        return "Hello World !!";
    }
}


 

Step 3 (Deploy the application in a Servlet Container)

 

We can deploy the webapp in a servlet container like Tomcat.

 

For this, Right Click on the project and choose Run As -> Run on Server and then choose the server( I am using Tomcat for this example) to deploy your project.

 

You can also use J2EE Preview or any other server for the same.

 

Alternatively, we can compile and package the application into a WAR using the command mvn clean package and then deploy the WAR in the servlet container.

 

Step 4 (Accessing the deployed resource)

 

We can access the deployed resource by using an utility like curl or by directly typing the resource url in the browser.
 
Here is the servlet mapping specified in the web.xml file :
 

    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/webapi/*</url-pattern>
    </servlet-mapping>

 
So, we can access the resource using the url:
 

http://localhost:8080/jersey-webapp/webapi/myresource

 

The browser should display :

Hello World !!

You can access the wadl using the following uri :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<application xmlns="http://wadl.dev.java.net/2009/02">
    <doc xmlns:jersey="http://jersey.java.net/" jersey:generatedBy="Jersey: 2.22.1 2015-10-07 10:54:41"/>
    <doc xmlns:jersey="http://jersey.java.net/" jersey:hint="This is simplified WADL with user and core resources only. To get full WADL with extended resources use the query parameter detail. Link: http://localhost:8080/jersey-webapp/webapi/application.wadl?detail=true"/>
    <grammars/>
    <resources base="http://localhost:8080/jersey-webapp/webapi/">
        <resource path="myresource">
            <method id="hello" name="GET">
                <response>
                    <representation mediaType="text/plain"/>
                </response>
            </method>
        </resource>
    </resources>
</application>

 

Understanding this program

 
MyResource class is annotated with @Path annotation. The @Path annotation identifies this class as the Service class.
 
Similarly, the service method hello() is annotated with @GET. We can also use other annotations like @POST, @PUT or @DELETE.
 
The @Produces annotation on the method informs the output type or representation in text/plain, text/html, text/json etc.
 
 

© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post

3 comments

  1. Venkatesh

    Please send me complete restful web services tutorial with examples

  2. […] Creating RESTful webservice using Jersey […]

Leave a Reply.. code can be added in <code> </code> tags

%d bloggers like this: