Monday, August 23, 2010

Jersey JAX-RS implementation


JAX-RS annotations

@Path Every Jersey resource has a URI pointing to it. A resource is identified with the @Path(“relative URI path”)annotation. This annotation is also used to define variables in URIs @Path(“relative URI path/{variable}”). Regular expression also allowed @Path("relative URI path/{variable:[a-zA-Z][a-zA-Z_0-9]}")

@GET, @PUT, @POST, @DELETE, and @HEAD are resource method designator annotations defined by JAX-RS and which correspond to the similarly named HTTP methods.

@Produces annotation is used to specify the MIME media types of representations a resource can produce and send back to the client. can be applied at both the class and method levels. This annotation works with @GET, @POST, and @PUT. if the value of the
HTTP header Accept is application/xml, the method handling the request returns
a stream of MIME type application/xml

@Consumes annotation is used to specify the MIME media types of representations a resource can consume. This annotation works together with @POST and @PUT. the client sets
the Content-Type HTTP header and the framework delegates the request to the
corresponding handling method.

@PathParam to extract a path parameter from the path component of the request URL. This annotation is used together with @Path and in conjunction with @GET, @POST,
@PUT, and @DELETE. we don't use the @PathParam annotation with a POST request.

@QueryParam is used to extract query parameters from the Query component of the request.

@FormParam is slightly special because it extracts information from a request representation that is of the MIME media type "application/x-www-form-urlencoded" and conforms to the encoding specified by HTML forms

@DefaultValue annotation is used to set default value to the query parameter if not exist.

@MatrixParam extracts information from URL path segments.

@HeaderParam extracts information from the HTTP headers.

@CookieParam extracts information from the cookies declared in cookie related HTTP headers

@Provider annotation is used for anything that is of interest to the JAX-RS runtime

@Context  ServletConfig, ServletContext, HttpServletRequest HttpServletResponse and SecurityContextare available using this annotation









Let’s start with creating project.
  1. create web project
  2. To add Jersey JARs (download link https://jersey.dev.java.net/)
asm-3.1.jar ,jsr311-api-3.1.jar ,jersey-bundle-1.3.jar,jersey-json-1.3.jar

3. Open the file "web.xml" and modify the file to the following.


<br />
<?xml version="1.0" encoding="UTF-8"?><br />
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><br />
<servlet><br />
<servlet-name>WebService</servlet-name><br />
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class><br />
<init-param><br />
<param-name>com.sun.jersey.config.property.packages</param-name> <param-value>org.resources</param-value> </init-param><br />
<load-on-startup>1</load-on-startup><br />
</servlet><br />
<servlet><br />
<servlet-name>ServletAdaptor</servlet-name><br />
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class><br />
<load-on-startup>1</load-on-startup><br />
</servlet><br />
<servlet-mapping><br />
<servlet-name>ServletAdaptor</servlet-name><br />
<url-pattern>/resources/*</url-pattern><br />
</servlet-mapping><br />
<servlet-mapping><br />
<servlet-name>WebService</servlet-name><br />
<url-pattern>/*</url-pattern><br />
</servlet-mapping><br />
<welcome-file-list><br />
<welcome-file>index.jsp</welcome-file><br />
</welcome-file-list><br />
</web-app><br />
<br />
<br />




The parameter "com.sun.jersey.config.property.package" defines in which package jersey will look for the web service classes. This property must point to your resources classes.

4. Create the following class.



package org.model;

import java.math.BigDecimal;
import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement
public class Product {

String name;
String description;
BigDecimal price;
int limit;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public BigDecimal getPrice() {
return price;
}

public void setProce(BigDecimal price) {
this.price = price;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public int getLimit() {
return limit;
}

public void setLimit(int limit) {
this.limit = limit;
}
}




package org.resources;

import com.sun.jersey.spi.resource.Singleton;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.model.Product;



@Path("/product/{name}")
@Singleton
public class ProductResource {

@GET
@Produces(MediaType.APPLICATION_XML)
public Product getXml(@PathParam("name") String name) {
Product p = new Product();
p.setName(name);
p.setDescription("descript");
p.setPrice(new BigDecimal(120));
return p;
}

@GET
@Produces(MediaType.APPLICATION_JSON)
public Product getJson(@PathParam("name") String name) {
Product p = new Product();
p.setName(name);
p.setDescription("descript");
p.setPrice(new BigDecimal(120));
return p;
}

@GET
@Produces(MediaType.TEXT_HTML)
public String getHtml(@PathParam("name") String name) {
Product p = new Product();
p.setName(name);
p.setDescription("descript");
p.setPrice(new BigDecimal(120));
return " " + "" + "Name: " + p.getName() + "
Description: " + p.getDescription() + "
Price: " + p.getProce() + "" + " ";
}

@Path("/allproducts")
@GET
@Produces(MediaType.APPLICATION_XML)
public List getProducts() {
List pl = new ArrayList();
Product p = new Product();
p.setName("Product x");
p.setDescription("descript");
p.setPrice(new BigDecimal(120));
pl.add(p);

p = new Product();
p.setName("Product x-2");
p.setDescription("descript-2");
p.setPrice(new BigDecimal(140));
pl.add(p);
return pl;
}
}


package org.resources;

import org.model.*;
import com.sun.jersey.spi.resource.Singleton;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;


@Path("/products")
@Singleton
public class ProductsResource {

@GET
@Produces(MediaType.APPLICATION_XML)
public List getXml(@DefaultValue("10") @QueryParam("limit") int limit) {
List pl = new ArrayList();
Product p = new Product();
p.setName("Product x");
p.setDescription("descript");
p.setPrice(new BigDecimal(120));
p.setLimit(limit);
pl.add(p);

p = new Product();
p.setName("Product x-2");
p.setDescription("descript-2");
p.setLimit(limit);
p.setPrice(new BigDecimal(140));
pl.add(p);

return pl;
}

@GET
@Produces(MediaType.TEXT_HTML)
public String getHtml() {
Product p = new Product();
p.setName("Product x");
p.setDescription("descript");
p.setProce(new BigDecimal(120));
return " " + "" + "Name: " + p.getName() + "
Description: " + p.getDescription() + "
Price: " + p.getProce() + "" + " ";
}
}








5. Now deploy in web server. I use Apache Tomcat v6.0


In the next post i will show you different way to test this service. 

1 comment:

  1. ClientResponse response = ws.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);

    I am getting the folllowing error

    The method accept(MediaType...) in the type WebResource is not applicable for the arguments (MediaType)

    Please advise.

    ReplyDelete

AWS Services

      1.         Identity Access Management (IAM): Used to control Identity (who) Access (what AWS resources).                   1....