Monday, August 9, 2010

RESTful Webservice with Jersey - Code example




This is a very basic RESTful Webservice done using Eclipse, Jersey Apache Tomcat. I used Eclipse 3.4.1, JDK 1.6 ,Tomcat V6 and Jersey 1.3 implements JAX-RS 1.1.

Steps:

1. In Eclipse create a "Dynamic Web Project" and project name is "MyFirstRESTFulWS".
2. Add the Jersey jars to class path.
3. Create a package "com.pu.test" and a java class "HelloJersey". This class is the resource which can be accessed by the URI.








4. HelloJersey java class


5. Change the web.xml



6.Deploy the Application in Tomcat and in the browser type the URI as follows:

http://localhost:8080/MyFirstRESTFulWS/rest/HelloWorld/John

And you will get the output as follows:

Saturday, August 7, 2010

RESTful Webservice and Jersey

When it comes to RESTful(Representational State Transfer) web services design, REST can be considered as set of constraints.In order to design a RESTful web service, resource identification, resource representation definition and URI definition need to be addressed for gathered requirements. In REST everything is a resource. In RESTful architecture the concept "resource" is simply anything that can be accessed/transferred between client and server. The resource representation is the resource exchange representation between client and server such as XML,JSON etc.In the HTTP Accept header the MIME media type can be found which accept by the client.In order to exchange resource representations a URI has to be defined. HTTP standard methods used in REST are CRUD which means CREATE(POST), RETREIVE(GET),UPDATE(PUT),DELETE(DELETE).
JAX-RS is the Sun's Java API for RESTful web services and Jersey is a JAX-RS reference implementation. Jersey hides the complexity in RESTful web service development by introducing annotations. Jersey resource means a java class in a web application context with a URI specified. The annotations and their usage is as follows:
@path - Defines the URI which is relative to the Web application context.If required parameters can also be added to the URI.

@Path("/testResource")
public class MyFirstResource {
//code
}


@Path("/testResource/{sampleParamter}")
public class MyFirstResource {
//code
}


@Context - Allows to access contextual objects in the class

@Context
Request request;


@PathParam - Retrieves the URI parameter values (Doesn't use with @POST)


public void newTodo(@FormParam("sampleParamter") String sampleParamterVal){
//code
}



HTTP Methods (The method name is not important, but we need to make sure to put a meaningful name)
@GET

@GET
public String getMethod() {
//code
}



@POST - The method comes with this annotation must have a input paramter to hold the HTTP request payload

@POST
public String postMethod(String payload) {
//code
}


@PUT - The method comes with this annotation must have a input paramter to hold the HTTP request payload

@PUT
public String putMethod(String payload) {
//code
}


@DELETE -

@DELETE
public String deleteMethod{
//code
}


NOTE: In order to access the URI parameter values inside HTTP methods new method parameters need to be added.

I/O formats

@Consume - Tells the Framework to delegate the correct method for the incoming request based on the Content Type defined in the HTTP request header.This is applicable only with @POST and @PUT.

@PUT
@Consumes(MediaType.APPLICATION_XML)
public Response incomingRepresentation(String payload) {
//code
}


@Produce - Tells the framework to send the defined type of resource representation to the client.

@GET
@Produces(MediaType.TEXT_PLAIN)
public String returnRepresentation() {
//code
}



In the next post i will provide a code example which helps you to understand Jersey annotations more clearly.