- 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
More ...............
- Download Jersey from http://jersey.java.net/
- Create a Dynamic Web Project(JerseyRest) in Eclipse
- 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
- jersey-client-1.4.jar
- jersey-core-1.4.jar
- Add the Jersey Servlet to Web.xml
<?xml version="1.0" encoding="UTF-8"?>
Note:
<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>- The Jersey Servlet maps to all requests under /rest
- The package where your service class resides is provided in the package-name init param.
/**
* @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;
}
} - 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();
}
}