Jersey – RESTful service

来源:互联网 发布:国内支持mac的网游 编辑:程序博客网 时间:2024/05/21 22:59

原文地址:http://techannotation.wordpress.com/2012/05/30/jersey-restful-service/


In one of my project, I used a Jersey framework to build a REST application. In this post I’ll introduce this lightweight framework that implements JAX-RS (JSR 311) references. If you are familiar with Spring MVC Rest you’ll find it very similar (obviously because both implement JAX-RS ).

First, we need the Jersey libraries to include.

jersey-bundle-1.8.jar
jersey-core-1.8.jar
jersey-server-1.8.jar
jersey-spring-1.8.jar

We can include it inside web.xml

<?xml version="1.0" encoding="UTF-8"?><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">  <context-param>   <param-name>contextConfigLocation</param-name>   <param-value>classpath:spring-easyrec.xml</param-value>  </context-param>  <listener>   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>     <servlet>    <servlet-name>Jersey</servlet-name>      <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>      <init-param>        <param-name>com.sun.jersey.config.property.packages</param-name>        <param-value>it.sample.rest</param-value>      </init-param>      <init-param>       <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>       <param-value>it.sample.filter.UserExtractionFilter</param-value>     </init-param>      <init-param>       <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>       <param-value>it.sample.filter.UserSetFilter</param-value>     </init-param>     <load-on-startup>1</load-on-startup>  </servlet>        <servlet-mapping>      <servlet-name>Jersey</servlet-name>      <url-pattern>/*</url-pattern>  </servlet-mapping>  </web-app>

The value of “com.sun.jersey.config.property.packages” is the java package that will be scanning for the rest annotation.

The value of “com.sun.jersey.spi.container.ContainerRequestFilters” is the class that implements com.sun.jersey.spi.container.ContainerRequestFilter interface. The only method that must be override is filter. This method will be invoked as first action at your request.


package it.sample.filter;import com.sun.jersey.spi.container.ContainerRequest;import com.sun.jersey.spi.container.ContainerRequestFilter;public class UserExtractionFilter implements ContainerRequestFilter {   @Override public ContainerRequest filter(ContainerRequest request) {    // Before Request processed  return request; }}
Now we have to define the controller for our path. This controller used an annotation like Spring MVC. In the follow example we can see a controller class with annotation for accept request at the url /sample/json/order/{orderid} and

package it.sample.rest;import javax.servlet.http.HttpServletResponse;import javax.ws.rs.CookieParam;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.Context;import javax.ws.rs.core.UriInfo;import com.sun.jersey.api.json.JSONWithPadding;@Path("sample")@Produces("application/javascript")public class ToDelete {      @Context    HttpServletResponse response = null;      @CookieParam("OrderCookie")    javax.ws.rs.core.Cookie orderCookie = null;        @Context    UriInfo info = null;        @GET    @Path("/order/json/{orderid}")    public JSONWithPadding orderProcessJson(@PathParam("orderid") String orderId)    {       return new JSONWithPadding("Processed!", "callback");    }      @GET    @Path("/order/xml/{orderid}")    @Produces("application/xml")    public JaxbOrder orderProcessXml(@PathParam("orderid") String orderId)    {        JaxbOrder jax = new JaxbOrder(orderId);        return jax;    }}

The JaxbOrder class is jaxb annotated class. When the client invoke the method “orderProcessXml” it will be serialized in xml.

In conclusion I think Jersey is a good alternative of Spring MVC and it’s still my first choice when I have to take decision how to build a RESTful service.

You can find more information at official Jersey web site http://jersey.java.net/



0 0