RESTful WS using Jersey Part 1 - Hello World

Here is the simple tutorial for RESTful Web Service using jersey implementation for RESTful Web Services. Java EE 6 has introduced jersey API as implementation for JSR-311 as API for RESTful Web Services. For ease of development,deployment and testing i have used NetBean 6.8 which supports JEE 6.

Jersey is an annotation based implementation. Here are the few important annotations used in this tutorial

@path(“/path/”) - a relative URI path indicating where the Java class will
be hosted

@GET - a request method designator and corresponds to HTTP Get method

@POST - a request method designator and corresponds to HTTP Post method

@Produces("text/plain") - specify the MIME media types of representations
a resource can produce

@Consumes("text/plain") -specify the MIME media types of representations
a resource can consume

@PathParam("User_Name") - a type of parameter that you can extract for use in your
resource class

@QueryParam("surname") - a type of parameter that you can extract from url query for use in your recourse class.

In Restful Web Services, each implementation is a resource and we can access its representational state using Get, Post, Put and Delete operations. Here we have implemented Get and Post operations.

In this post i am going to demo developing, deploying and testing RESTful services using Jersey APIs in Netbean 6.8

1. Create a Java Web Application

Create Java Web Application - Net Beans 6.8

2. Specify Context path at end of this wazard

Context Path - NetBeans 6.8

At the end of this wizard, you will find a standard web application structure under project

3. Create a web.xml file if NetBean didn’t create it

3

This wizard creates empty standard web.xml file under WEB-INF. Update this file with Jersey default adaptor for restful services

<servlet>
        <servlet-name>ServletAdaptor</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>ServletAdaptor</servlet-name>
        <url-pattern>/resources/*</url-pattern>
    </servlet-mapping>

updating this mapping file in web.xml , we are telling web application to route all url requests with given format (/resources/*) to ServletContainer. This servlet will take care of handling all available resources.

4. Create Restful Web Service using wizard

4

Mention all required info during this wizard

6

Here resource package name, path ,MIME type and resource name has been defined. Path’s value will appear to resource class file as annotations. MIME type will define type response/request generated/accepted by this service. This info will also appear as annotations.

5. After all this steps complete project will be like

8

6. Updating Resource class – modify get method so that it will return a string on invoke. Here is the complete code for resource class

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package demo.resorces;

import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PathParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;

/**
* REST Web Service
*
* @author Yogesh
*/

@Path("hello")
public class HelloResource {
    @Context
    private UriInfo context;

    /** Creates a new instance of HelloResource */
    public HelloResource() {
    }

    /**
     * Retrieves representation of an instance of demo.resorces.HelloResource
     * @return an instance of java.lang.String
     */
    @GET
    @Produces("text/plain")
    public String getText() {
        //TODO return proper representation object
        return "Hello from GetText";
    }

    /**
     * PUT method for updating or creating an instance of HelloResource
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @PUT
    @Consumes("text/plain")
    public void putText(String content) {
    }
}

Here getText method is annotated with @Get and @Produces tags , it means it will get invoked on HTTP Get request and produces simple plain text.

7. Running Project - To deploy and test this service we need update project properties so that on running this project it will launch appropriate url

7

8. Right click on project and run , you will see output as shown in below

9

9. Now modify getText method to produce html output instead of plain text.

@GET
   //@Produces("text/plain")
   @Produces("text/html")
   public String getText() {
       //TODO return proper representation object
       //return "Hello from GetText";
       return "<html><body><h1>Hello from GetHtml</h1></body></html>";
   }

10. After running project with updated getText method

10

Here we got html output.

Comments

Popular posts from this blog

State Design Pattern by Example

Eclipse command framework core expression: Property tester

Composite Design Pattern by example