Jersey – RESTful service

原文地址: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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值