- 浏览: 1190446 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (883)
- Spring (101)
- Swing (1)
- Mysql (21)
- Js (59)
- Jsp (2)
- Hibernate (19)
- Myeclipse (5)
- SqlServer 2000 (2)
- poi (15)
- Java (70)
- SSH (12)
- Html (47)
- Fusion Charts (1)
- C\C++ (2)
- 汇编 (36)
- Jquery (37)
- Struts2 (6)
- Ubuntu (16)
- powerDesinger (4)
- Jboss (3)
- JAX-RS (13)
- JAXB (5)
- JAX-WS (11)
- JMS (4)
- WebSocket (8)
- PHP (16)
- Flash (1)
- maven (3)
- Oracle (8)
- HttpClient (6)
- sqlserver (19)
- svn (5)
- Tomcat (3)
- Jdbc (3)
- EsayUi (11)
- 微信公众平台 (19)
- IIS (2)
- Freemarker (11)
- Comet (1)
- Spring MVC (85)
- JBoss Seam (3)
- 二维码 (9)
- Spring Security (4)
- Ehcache (3)
- Apache Shiro (7)
- jackson (16)
- JPA (8)
- jcaptcha (2)
- RSA (2)
- Ajax (6)
- 跟我学Shiro (0)
- Spring4 (19)
- 跟我学spring3 (0)
- css (32)
- excel (4)
- Filter (3)
- 微信公众帐号开发教程 (0)
- Android (6)
- log4j (6)
- 淘宝接口 (17)
- 支付集成 (3)
- 单点登录 (3)
- Html5 (27)
- 移动平台前端 (3)
- Linux (44)
- FusionCharts (27)
- Json Jackson Xml (5)
- 方培工作室-微信开发 (0)
- Apache与Tomcat与IIS整合 (10)
- Nginx (17)
- webService (2)
- apache (4)
- lucene (3)
- lodop (2)
- Shiro (3)
- zTree (2)
- ireport (12)
- Servlet3.0 (5)
- 前端美工 (19)
- AngularJS (1)
- C#开发微信门户及应用 (0)
- Shell (3)
- bat脚本 (16)
- Bootstrap (26)
- Less (10)
- photoshop (6)
- Redis (6)
- Mongodb (10)
- MyBatis (3)
- 数据结构 (0)
- 读写分离-主从复制 (0)
- JFinal (0)
- 百度地图api (3)
- hadoop-hbase-hive-spark (3)
- WebStorm (2)
- Quartz (5)
- ios (0)
- Mina (8)
- Android Studio (4)
- Ratchet教程 (0)
- 移动端重构系列 (1)
- cubic-bezier贝塞尔曲线CSS3动画工具 (1)
- nginx+tomcat+memcached集群 (0)
- 集群 (0)
- ZooKeeper (3)
- Dubbo (0)
- vpn (0)
- kafka (0)
- JVM垃圾回收机制 (0)
- 微信小程序 (0)
- Lua (0)
- Hystrix (0)
- Vue.js (0)
- mycat (0)
- Openresty (0)
- springBoot (0)
- 新分类 (0)
- guava (0)
- 大数据 (0)
- Sentinel (0)
最新评论
-
JackMacing:
中文怎么解决?
SpringMVC与iReport(JasperReports) 5.6整合开发实例 -
18335864773:
用pageoffice把.可以实现在线的文档操作.直接转pdf ...
转:使用jasperreport动态生成pdf,excel,html -
linhao0907:
推荐一款轻量开源的支付宝组件:https://github.c ...
关于Alipay支付宝接口(Java版) -
songronghu:
太好了,非常有用,谢谢分享~
Java ConcurrentModificationException 异常分析与解决方案 -
wzwahl36:
http://www.atool.org/json2javab ...
Java下利用Jackson进行JSON解析和序列化
一、Annotations
- @javax.ws.rs.PathParam: 从URI模板参数中提取数据
- @javax.ws.rs.MatrixParam:从URI中提取Matrix参数
- @javax.ws.rs.QueryParam:从URI中提取查询参数
- @javax.ws.rs.FormParam:提取Post Form参数
- @javax.ws.rs.HeaderParam:提取HTTP请求头信息
- @javax.ws.rs.CookieParam:提取客户设置的cookie的信息
- @javax.ws.rs.core.Context:通用的注入annotation,允许注入各种帮助或者信息对象
通常这些注释用在服务方法上,当JAX-RS收到一个请求会,就会去查找相应的服务方法,然后把方法需要的信息注入。
如果是 “每个请求一个对象”的模式,你可以将这些annotation用在变量、set方法或者是构造方法上;如果是单态模式,则不允许将这些annotation用在变量、或者set方法上,因为对像会同时处理多个请求,如果将这些值用在变量或者set方法上,则多个请求会彼此冲突,陷入错误。
二、PathParam
- public class CustomerResource {
- ...
- @Path("{id}")
- @GET
- @Produces("application/xml")
- public StreamingOutput getCustomer(@PathParam("id") int id) {
- ...
- }
- }
此处,取得{id}的值,并试图转换成一个int型的值。
可以同时使用多个PathParam:
- @Path("/customers")
- public class CustomerResource {
- ...
- @Path("{first}-{last}")
- @GET
- @Produces("application/xml")
- public StreamingOutput getCustomer(@PathParam("first") String firstName,
- @PathParam("last") String lastName) {
- ...
- }
- }
PathParam的范围:总是引用最接近的PathParam的值,例如:
- @Path("/customers/{id}")
- public class CustomerResource {
- @Path("/address/{id}")
- @Produces("text/plain")
- @GET
- public String getAddress(@PathParam("id") String addressId) {...}
- }
例如HTTP请求为:GET /customers/123/address/456 , 则 addressId 的值为456.
注入PathSegment
PathParam除了可以注入Path参数,也可以注入一个javax.ws.rs.core.PathSegment实便;PathSegment是一个特定Path片段的抽象,如下:
- package javax.ws.rs.core;
- public interface PathSegment {
- String getPath(); //具体的URI的path片段值,去除了所有的matrix参数
- MultivaluedMap<String, String> getMatrixParameters(); //该path片段拥有的所有的matrix值
- }
然后如下使用:
- @Path("/cars/{make}")
- public class CarResource {
- @GET
- @Path("/{model}/{year}")
- @Produces("image/jpeg")
- public Jpeg getPicture(@PathParam("make") String make,
- @PathParam("model") PathSegment car,
- @PathParam("year") String year) {
- String carColor = car.getMatrixParameters().getFirst("color");
- ...
- }
- }
例如:GET /cars/mercedes/e55;color=black/2006。则 make是mercedes;model是e55;year是2006;color是black。
注入多个PathSegment
如果对对应有Path含有多个path片段,则需要注入多个PathSegments类,例如:
- @Path("/cars/{make}")
- public class CarResource {
- @GET
- @Path("/{model : .+}/year/{year}")
- @Produces("image/jpeg")
- public Jpeg getPicture(@PathParam("make") String make,
- @PathParam("model") List<PathSegment> car,
- @PathParam("year") String year) {
- }
- }
其中请求可能是:GET /cars/mercedes/e55/amg/year/2006。这里model对应的path片段为:/e55/amg。所以car变量中含有两个PathSegment对象。
用代码获取URI的信息
有时候可能想通过程序的方式获取URI中的信息,而不使用PathParam注释。这里我们需要通过接口javax.ws.rs.core.UriInfo接口去获取这些信息,UriInfo接口定义如下:
- public interface UriInfo {
- public String getPath(); //返回匹配的相对uri路径
- public String getPath(boolean decode); //返回解码后的相对uri路径
- public List<PathSegment> getPathSegments(); //返回path片段
- public List<PathSegment> getPathSegments(boolean decode); //返回解码后的path片段
- public MultivaluedMap<String, String> getPathParameters(); //返回PathParam表
- public MultivaluedMap<String, String> getPathParameters(boolean decode); //同上
- ...
- }
要获取UriInfo对象,就需要用到@javax.ws.rs.core.Context注释了。例如:
- @Path("/cars/{make}")
- public class CarResource {
- @GET
- @Path("/{model}/{year}")
- @Produces("image/jpeg")
- public Jpeg getPicture(@Context UriInfo info) {
- String make = info.getPathParameters().getFirst("make");
- PathSegment model = info.getPathSegments().get(1);
- String color = model.getMatrixParameters().getFirst("color");
- ...
- }
- }
三、MatrixParam
除了上面介绍的使用PathSegment去获取MatrixParam值外,我们也可以直接使用@MatrixParam去获取值,这样来得更直接、简洁,例如:
- @Path("/{make}")
- public class CarResource {
- @GET
- @Path("/{model}/{year}")
- @Produces("image/jpeg")
- public Jpeg getPicture(@PathParam("make") String make,
- @PathParam("model") String model,
- @MatrixParam("color") String color) {
- ...
- }
- }
不过如果Path中含有多个同名的MatrixParam,则还是需要使用PathSegment来获取,例如:GET /mercedes/e55;color=black/2006/interior;color=tan
四、@QueryParam
很显然,QueryParam用来获取查询参数,对于 GET /customers?start=0&size=10 ,例如:
- @Path("/customers")
- public class CustomerResource {
- @GET
- @Produces("application/xml")
- public String getCustomers(@QueryParam("start") int start,
- @QueryParam("size") int size) {
- ...
- }
- }
这里start为0,size为10.
同上面的PathParam,也可以用UriInfo去获取QueryParam,例如:
- @Path("/customers")
- public class CustomerResource {
- @GET
- @Produces("application/xml")
- public String getCustomers(@Context UriInfo info) {
- String start = info.getQueryParameters().getFirst("start");
- String size = info.getQueryParameters().getFirst("size");
- ...
- }
- }
五、@FormParam
很自然,FormParam用于提取POST请求中的Form参数,其中Content-Type被假设为application/x-www-formurlencoded。例如有以下Form请求
- <FORM action="http://example.com/customers" method="post">
- <P>
- First name: <INPUT type="text" name="firstname"><BR>
- Last name: <INPUT type="text" name="lastname"><BR>
- <INPUT type="submit" value="Send">
- </P>
- </FORM>
可以如下取值:
- @Path("/customers")
- public class CustomerResource {
- @POST
- public void createCustomer(@FormParam("firstname") String first,
- @FormParam("lastname") String last) {
- ...
- }
- }
六、HeaderParam
很直接,用来提取HTTP Header值的。例如:
- @Path("/myservice")
- public class MyService {
- @GET
- @Produces("text/html")
- public String get(@HeaderParam("Referer") String referer) {
- ...
- }
- }
如果想提取所有的header值,那就需要用到javax.ws.rs.core.HttpHeaders接口了:
- public interface HttpHeaders {
- public List<String> getRequestHeader(String name);
- public MultivaluedMap<String, String> getRequestHeaders();
- ...
- }
例如方法同上面的PathSegment,也是用context去获取,例如:
- @Path("/myservice")
- public class MyService {
- @GET
- @Produces("text/html")
- public String get(@Context HttpHeaders headers) {
- String referer = headers.getRequestHeader("Referer").get(0);
- for (String header : headers.getRequestHeaders().keySet())
- {
- System.out.println("This header was set: " + header);
- }
- ...
- }
- }
七、@CookieParam
提取cookie信息,例如:
- @Path("/myservice")
- public class MyService {
- @GET
- @Produces("text/html")
- public String get(@CookieParam("customerId") int custId) {
- ...
- }
- }
这里注入了的是一个cookie的值,如果想取得更多的信息,而不公公是基本值,则可以直接注入javax.ws.rs.core.Cookie对象,例如:
- @Path("/myservice")
- public class MyService {
- @GET
- @Produces("text/html")
- public String get(@CookieParam("customerId") Cookie custId) {
- ...
- }
- }
Cookie类具有一些其他的方法可以用来获取更多信息,例如:
- package javax.ws.rs.core;
- public class Cookie
- {
- public String getName() {...}
- public String getValue() {...}
- public int getVersion() {...}
- public String getDomain() {...}
- public String getPath() {...}
- ...
- }
也可以用javax.ws.rs.core.HttpHeaders获取所有的cookie:
- @Path("/myservice")
- public class MyService {
- @GET
- @Produces("text/html")
- public String get(@Context HttpHeaders headers) {
- for (String name : headers.getCookies().keySet())
- {
- Cookie cookie = headers.getCookies().get(name);
- System.out.println("Cookie: " +
- name + "=" + cookie.getValue());
- }
- ...
- }
- }
发表评论
-
JAX-RS入门 十二: 可伸缩的JAX-RS应用
2012-09-11 19:10 643一、HTTP Caching HTTP1.0中定义了E ... -
JAX-RS入门 十一:HATEOAS
2012-09-11 19:09 697HATEOAS全称:超媒体即状态应用引擎 (Hypermedi ... -
JAX-RS入门 十: 兼容与演进
2012-09-11 19:09 820网络服务会随着时间 ... -
JAX-RS入门 九: 内容约定(2)
2012-09-10 23:33 597JAX-RS与Conneg JAX-RS有一些用来帮助 ... -
JAX-RS入门 九: 内容约定(1)
2012-09-10 23:32 629通常一个SOA的应用都需要足够的灵活,以便于各种不同的客户端或 ... -
JAX-RS入门 八: HTTP响应
2012-09-10 23:32 965首先先看一下HTTP中定义的响应码及其意义: ... -
JAX-RS入门 七: 数据处理(2)
2012-09-10 23:31 840上节介绍了JAX-RS中怎么与一些基础的对象进行交互,这节介绍 ... -
JAX-RS入门 六: 数据处理(1)
2012-09-10 23:30 880接下来要花两小节来介绍一下JAX-RS中的数据处理(Data ... -
JAX-RS入门 五: 自动类型转换
2012-09-09 12:41 682一、默认类型转换规则 在上一节中,已经了解了怎么使用那 ... -
JAX-RS入门 三 :细节
2012-09-09 12:33 721一、若希望一个Java类能够处理REST请求,则这个类必须 ... -
JAX-RS入门 二 :运行
2012-09-09 12:31 935上一节,已经成功的定义了一个REST服务,并且提供了具体的 ... -
JAX-RS入门 一 :基础
2012-09-09 12:29 1687简介 JAX-RS是一套用java实现REST服务的规范,提 ...
相关推荐
[奥莱理] RESTful Java 开发 (Jax-RS 实现) (英文版) [奥莱理] RESTful Java with Jax-RS (E-Book) ☆ 图书概要:☆ Learn how to design and develop distributed web services in Java using RESTful ...
The RESTEasy workbook that follows provides step-by-step instructions for installing, configuring, and running several working JAX-RS examples, using the JBoss RESTEasy implementation of JAX-RS 2.0. ...
使用到的外部jar包有(必须的部分,需要加到Web容器中) •neethi-3.0.2.jar •jsr311-api-1.1.1.jar •cxf-bundle-2.6.0.jar 使用到的外部jar包有(可选的部分,当且仅当作为一个独立的application运行时) ...
赠送jar包:jackson-jaxrs-json-provider-2.7.8.jar; 赠送原API文档:jackson-jaxrs-json-provider-2.7.8-javadoc.jar; 赠送源代码:jackson-jaxrs-json-provider-2.7.8-sources.jar; 赠送Maven依赖信息文件:...
javax.xml.ws.Service 报错需要的包,亲测可以用,直接下载在ide里buildpath一下就可以,四个jar包 ,整合了其他的jar所以配置简单
JAX-RS(JSR 311)指的是Java API for RESTful Web Services,Roy Fielding也参与了JAX-RS的制订,他在自己的博士论文中定义了REST。对于那些想要构建RESTful Web Services的开发者来说,JAX-RS给出了不同于JAX-WS...
概述这是一个多模块项目,包含用于以下数据格式的基于Jackson的JAX-RS提供程序:(二进制JSON) (另一种二进制JSON) 提供程序实现特定数据格式的JAX-RS MessageBodyReader和MessageBodyWriter处理程序。...
关于JAX-RS的注解详细,本人通过网络及实际应用收集的资料
使用Eclipse开发基于SpringBoot+JAX-RS的Restful服务.docx使用Eclipse开发基于SpringBoot+JAX-RS的Restful服务.docx使用Eclipse开发基于SpringBoot+JAX-RS的Restful服务.docx使用Eclipse开发基于SpringBoot+JAX-RS的...
jax-rs 2.1规范最终版,英文原版。jax-rs规范定义了java平台Rest调用风格的WebService接口。
srb4j, Simple RESTFul backend for Java, 一个JAX-RS服务端代码脚手架(就像AppFuse之于Webapp),基于OAuth2提供了完整的注册、登录、密码恢复等功能。 它本质是一个Maven Archetype, 所以你可以用它快速生成一个...
JAX-RS REST helloworld 项目源码 下载后引入eclipse jee 启动tomcat可直接访问 访问路径为http://localhost:8080/AndroidJAX-RS/jaxrs/helloworld/html
restlet_jax-rs列子 可直接运行 在我的博客里有一些描述。希望能给大家提供点帮助
web service rest(jax-rs)整合完整的Jax-rs组件包,内含2个版本,分别是1.0和1.4版本。
JAX-RS-FileUpload-Jersey-Example
JAX-RS+spring
JAX-RS 性能 比较每个请求和单例 JAX-RS 资源类的性能。 请参阅。 该项目包括 4 个资源类:2 个每个请求范围和 2 个单例。 两个只是返回一个查询参数。 在另外两个资源中,我们将通过@Context 注入 UriInfo。 上...
RESTful Java with JAX-RS 2.0.pdf
JAX-RS-messanger-服务 使用JAX-RS的消息传递REST服务