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/
- Jersey – RESTful service
- RESTFUL Service : based on Jersey
- jersey (RESTful Web Service框架)
- MyEclipse + Jetty + Jersey 创建RESTFul service
- 使用 Jersey + Tomcat 輕鬆開發 RESTful Web Service
- Restful 和 Jersey介绍(Web Service )
- RESTful Web Service框架jersey简单总结
- 使用jersey构建Restful web service
- java利用Jersey创建RESTful service
- jersey 基于tomcat web service 开发 restful service
- jersey RESTful
- Build a RESTful Web service using Jersey and Apache Tomcat
- Jersey 2.17 + Tomcat + Maven 构建restful service 实例
- Jersey 2.17 + Tomcat + Maven 构建restful service 实例
- Jax-rs\Jersey框架与RESTful Web Service
- Jax-rs\Jersey框架与RESTful Web Service
- Jersey 2.17 + Tomcat + Maven 构建restful service 实例
- Jersey 2 和 Spring 4 构建 RESTful web service
- 顺序栈、链栈、顺序队和链队小结
- 生活中一些很有意思的小技巧
- wpf 后台绑定数据 binding
- 关于答辩问题收集
- ajax的定时调用
- Jersey – RESTful service
- OpenGL ES VBO 和 VAO
- 日拱一卒之C++基础一 指针与引用基本概念
- Oracle Job的创建方法(不积跬步,无以至千里)
- [LeetCode] Palindrome Number
- java web中的session属性范围和request属性范围
- Linux上安装icescrum
- asmca命令行
- PHP错误Can't use method return value in write context