Jersey – RESTful service
2014-05-26 14:30
190 查看
原文地址: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.
We can include it inside web.xml
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.
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/
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.
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/
相关文章推荐
- Web编程学习五: 使用Jersey和JPA来创建RESTful WebService
- Jersey 2 和 Spring 4 构建 RESTful web service
- Jersey 2.17 + Tomcat + Maven 构建restful service 实例
- 基于Jersey框架搭建Restful Java Web Service的基本步骤
- Build a RESTful Web service using Jersey and Apache Tomcat
- 基于RESTful Web Service 的Jersey框架与Spring完美整合
- 使用SpringBoot整合Jersey 实现Restful webservice.可以同时使用springmvc。
- RESTFUL Service : based on Jersey
- Jersey Restful WebService 接收 JSON 数组 对象
- RESTFUL Service : based on Jersey
- Jersey 2.17 + Tomcat + Maven 构建restful service 实例
- Jersey 2.17 + Tomcat + Maven 构建restful service 实例
- Jersey(1.19.1) - Deploying a RESTful Web Service
- Web编程学习四: 使用Jersey来创建RESTful WebService
- Quick Start - WCF 4.0 RESTful Service Setup
- 用Jersey构建RESTful服务2--JAVA对象转成XML输出
- 用Jersey构建RESTful服务4--通过jersey-client客户端调用Jersey的Web服务模拟CURD
- 使用 Jersey 和 Apache Tomcat 构建 RESTful Web 服务
- 用Jersey、Grizzly开发第一个RESTful服务helloworld
- 概念梳理:tcp http soap rpc socket websocket rmi webservice RESTful