@PathParam是一个参数注解,允许你映射URI路径片断变量到你的方法调用。
@Path("/library") public class Library { @GET @Path("/book/{isbn}") public String getBook(@PathParam("isbn") String id) { // search my database and get a string representation and return it } }
这允许你在你的资源当中,在你的URI内嵌入变量识别。在上面的示例,我们需要访问的书本信息通过一个URI参数isbn,你注入的该参数的类型可以是基本类型,一个字符串,或者任何拥有接收一个字符串参数构造方法的Java对象,或一个静态的接收字符串参数的valueOf方法。示例中,比方说我们希望isbn是一个真正的对象,我们可以这样做:
@GET @Path("/book/{isbn}") public String getBook(@PathParam("isbn") ISBN id) {...} public class ISBN { public ISBN(String str) {...} }
或者代替公共的String构造方法,拥有一个valueOf方法:
public class ISBN { public static ISBN valueOf(String isbn) {...} }
5.1. 高级 @PathParam 和正则表达式
@PathParams有一些更复杂的使用在上一章节中没有讨论。
你可以指定一个或多个路径参数嵌入到一个URI片断,下面是一些示例:
1. @Path("/aaa{param}bbb") 2. @Path("/{name}-{zip}") 3. @Path("/foo{name}-{zip}bar")
所以,一个URI "/aaa111bbb" 将匹配 #1. "/bill-02115" 将匹配 #2. "foobill-02115bar" 将匹配#3.
之前我们讨论了你能够如何使用正则表达式到@Path的value:
@GET @Path("/aaa{param:b+}/{many:.*}/stuff") public String getIt(@PathParam("param") String bs, @PathParam("many") String many) {...}
下面的这些请求,让我们看看"param" 和 "many" 将会是哪些值:
Table 5.1.
Request |
param |
many |
GET /aaabb/some/stuff |
bb |
some |
GET /aaab/a/lot/of/stuff |
b |
a/lot/of |
5.2. @PathParam 和 PathSegment
关于URI路径调用规范有一个非常简单抽象的片断检查,在
javax.ws.rs.core.PathSegment:
public interface PathSegment { /** * Get the path segment. * <p> * @return the path segment */ String getPath(); /** * Get a map of the matrix parameters associated with the path segment * @return the map of matrix parameters */ MultivaluedMap<String, String> getMatrixParameters(); }
你要以在resteasy注入一个
PathSegment代替你的
@PathParam的value:
@GET @Path("/book/{id}") public String getBook(@PathParam("id") PathSegment id) {...}
这是非常有用的,如果你有一堆的
@PathParam使用矩阵参数。矩阵参数的想法是,它们是嵌入在URI路径片断的任意一对name-value对集合。PathSegment对象允许您访问这些参数,也看到
MatrixParam。
一个矩阵参数的例子:
GET http://host.com/library/book;name=EJB 3.0;author=Bill Burke
矩阵参数的基本思想是,它代表的资源是可寻址的,来自于它们的属性以及它们的原始id。
相关推荐
Resteasy + Spring + Netty sample Inject resteasy provider / controllers as spring bean Authentication Run at Main.java Test http://localhost:8082/resteasy/hello/world 教程 jax-rs规范用法: ...
resteasy
Spring整合RestEasy示例工程源码,Spring+Jetty+RestEasy
resteasy讲解
该资源是java restful的应用框架resteasy---实现了jax-rs规范的一个demo,如有疑问请联系caizhh2009@gmail.com
resteasy入门级文档、如何使用Restful进行WebService的开发简介及相关RestEasy相关资源的链接
resteasy不使用tomcat使用netty实现,实现了高性能,也可以不需要tomcat的容器,一个主函数就能执行
resteasy简单demo使用的jar包
详细描述了restEasy在servlet3.0前后的不同使用方法;restEasy 结合httpServer 无需容器开发;restEasy的通用差错拦截;附详细实现源码!
RESTEasy入门经典.docx
使用RESTEasy+guice实现上传下载文件
resteasy-jaxrs-2.3.2框架源码。希望您了解Resteasy框架有所帮助
RESTEasy JSON DEMO
resteasy参考文档及jar包
resteasy-reference-guide-en-US.pdf
JBoss RESTEasy 是一个用来使用Java语言开发 RESTFul Web服务的框架。RESTEasy 项目是 JAX-RS 的一个实现,集成的一些亮点: * 不需要配置文件,只要把JARs文件放到类路径里面,添加 @Path 注解就可以了。 * 完全...
Resteasy英文文档,开发rest风格的API接口
介绍了RESTEasy中的安全机制的使用方法
案例基于 jboss 7.1.0 和restEasy3.0.18,是目前最新的,因为在网上看到restEasy的例子都比较旧了,所以想到自己写一个案例。直接进入正题。 因为使用的是jboss7,所以restEasy已经集成在里面了,直接用就行,甚至...
resteasy上传功能资料 都是网上搜集的一些代码demo 方便学习入门