Java Search

Monday, March 28, 2011

Resful Web Services With Jersey API

This post describes how to use Jersey API to create RESTful Web Services. To keep it simple, I'm not going describe how REST works or should be implemented. Rather, it is simple example on how to use the API to implement some of the more common features of a REST service. The post describes how to
  • Create a RESTful Web Service using Jersey
  • Create GET, POST, PUT and DELETE handlers in the service
  • Create a Jersey Client to invoke the various methods on the Service
The example here has been tested on Tomcat7, with Java SE 6. The following steps explain how to use the API
More ...............
  1. Download Jersey from http://jersey.java.net/
  2. Create a Dynamic Web Project(JerseyRest) in Eclipse
  3. Add the Jersey Jar files to the classpath. The following files were used:http://www.blogger.com/img/blank.gif
    Server:
    • asm-3.1.jar
    • jersey-core-1.4.jar
    • jersey-server-1.4.jar
    Client:
    • jersey-client-1.4.jar
    • jersey-core-1.4.jar
  4. Add the Jersey Servlet to Web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>JerseyRest</display-name>
    <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
    <param-name>com.sun.jersey.config.property.packages</param-name>
    <param-value>com.blogspot.aoj.restws</param-value>
    </init-param>

    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/*</url-pattern>
    </servlet-mapping>
    </web-app>
    Note:
    • The Jersey Servlet maps to all requests under /rest
    • The package where your service class resides is provided in the package-name init param.
    Create the Service Class
    /**
    * @author Abhi Vuyyuru
    */

    package com.blogspot.aoj.restws;

    import javax.ws.rs.Consumes;
    import javax.ws.rs.DELETE;
    import javax.ws.rs.FormParam;
    import javax.ws.rs.GET;
    import javax.ws.rs.POST;
    import javax.ws.rs.PUT;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;

    @Path("/restws")
    public class RestWS {
    @GET
    @Produces("text/plain")
    @Path("hello")
    public String sayHello() {
    return "Hello World";
    }

    @GET
    @Path("concat/i/{i}/j/{j}")
    @Produces("text/plain")
    public String concat(@PathParam("i") String i, @PathParam("j") String j) {
    System.out.println("GET Input : " + i + j);
    return (i + j);
    }

    @POST
    @Path("posts")
    @Produces(MediaType.TEXT_HTML)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public String consumeReq(@FormParam("i") String i, @FormParam("j") String j) {
    System.out.println("POST Input : " + i + j);
    return i + j;
    }

    @PUT
    @Path("puts")
    @Produces(MediaType.TEXT_HTML)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public void putReq(@FormParam("i") String i, @FormParam("j") String j) {
    System.out.println("PUT Input : " + i + j);
    return;
    }

    @DELETE
    @Path("dels/i/{i}/j/{j}")
    public void deleteReq(@PathParam("i") String i, @PathParam("j") String j) {
    System.out.println("DELETE Input : " + i + j);
    return;
    }

    }
  5. Create the Client
    /**
    * @author Abhi Vuyyuru
    */
    package com.blogspot.aoj.clients;

    import java.io.IOException;

    import javax.ws.rs.core.MultivaluedMap;

    import com.sun.jersey.api.client.Client;
    import com.sun.jersey.api.client.WebResource;
    import com.sun.jersey.core.util.MultivaluedMapImpl;

    public class RestWSClient {

    public static void main(String[] args) throws IOException {
    Client client = Client.create();
    WebResource wsClient = client.resource("http://localhost:8080/JerseyRest/restws/");

    String helloResp = wsClient.path("hello").get(String.class);
    System.out.println(helloResp);

    MultivaluedMap queryParams = new MultivaluedMapImpl();
    queryParams.add("i", "val1");
    queryParams.add("j", "val2");
    String postResp = wsClient.path("posts").post(String.class, queryParams);
    System.out.println(postResp);

    String getResp = wsClient.path("concat").path("i/" + "val1").path("j/" + "val2").get(String.class);
    System.out.println(getResp);

    wsClient.path("puts").put(queryParams);

    wsClient.path("dels").path("i/" + "val1").path("j/" + "val2").delete();

    }
    }

No comments:

Post a Comment

Popular Posts