RestEasy+用户指南----第5章.@PathParam
来源:互联网 发布:汽车导航软件免费下载 编辑:程序博客网 时间:2024/05/18 04:57
@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 }}
(很简单,当你发出get请求 /book/152-963参数152-963就在isbn中存储着,然后交给变量id,这样你的方法就算是成功的接收了该参数)
这将允许你在uri中内嵌一个变量标识符。在上边的例子中,参数isbn被用来传递book的信息。你所嵌入的数据类型可以是任何元数据类型,例如String,具有String参数的构造函
数的一个类对象,或者a static valueOf method that takes a String as a parameter。例如,假设ISBN是一个对象,我们可以
@GET @Path("/book/{isbn}") public String getBook(@PathParam("isbn") ISBN id) {...} public class ISBN { public ISBN(String str) {...} }
或者是一个public方法String构造,包含一个valueOf 方法
public class ISBN { public static ISBN valueOf(String isbn) {...} }
(运行中应该能够自动调用类的valueOf方法进行转换,对java不是很熟悉,我想大概应该是这样)
5.1. @PathParam深入 以及正则表达式
下边是一些更复杂的应用,这些在前边的章节并没有讨论
你可以指定一个或者多个参数用以内嵌到你的uri中,下边是一些例子
1.@Path("/aaa{param}bbb")
2.@Path("/{name}-{zip}")
3.@Path("/foo{name}-{zip}bar")
那么,路径 "/aaa111bbb" 将会匹配#1. "/bill-02115"将会匹配 #2. 路径"foobill-02115bar" 将会匹配 #3.
之前,我们已经讨论过如何在@Path中使用正则表达式
@GET@Path("/aaa{param:b+}/{many:.*}/stuff")public StringgetIt(@PathParam("param") String bs, @PathParam("many")String many) {...}
在如下的请求中,我们可以了解到“param”以及“many”值是多少
Request param many
GET /aaabb/some/stuff
bb
some
GET /aaab/a/lot/of/stuff
b
a/lot/of
5.2@PathParam 和 PathSegment
Thespecification has a very simple abstraction for examining a fragment of the URIpath being invoked on 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而不是用一个值
@GET @Path("/book/{id}") public String getBook(@PathParam("id") PathSegment id) {...}
当你使用matrix parameters传递诸多参数时,浙江爱那个非常有用。你可以将任意个name和value的键值对潜入到uri path segment中。PathSegment对象将会负责去获取这些参数。
你也可以看一下MatrixParam(后边会讲到)
一个matrix parameter的例子是
GEThttp://host.com/library/book;name=EJB 3.0;author=Bill Burke
Thebasic idea of matrix parameters is that it represents resources that areaddressable by their attributes as well as their raw id.
(好吧,5.2我表示不怎么理解,自己还没有试过这个部分。)
- RestEasy+用户指南----第5章.@PathParam
- RestEasy用户指南---第6章.@QueryParam
- RestEasy 用户指南----第7章 @HeaderParam
- RestEasy 用户指南---第11章 @FormParam
- RestEasy 用户指南----第4章.使用@Path @GET @POST 等
- RestEasy+用户指南+翻译索引
- RESTEasy数据自动装配之@PathParam
- jbpm用户指南翻译:第5章 部署
- RESTEasy:@FormParam、@PathParam、@QueryParam、@HeaderParam、@CookieParam、@MatrixParam说明
- RESTEasy:@FormParam、@PathParam、@QueryParam、@HeaderParam、@CookieParam、@MatrixParam说明
- RESTEasy:@FormParam、@PathParam、@QueryParam、@HeaderParam、@CookieParam、@MatrixParam说明
- 《gradle 用户指南》中文版 第5章 Gradle控制台
- Jbpm用户指南翻译:第10章 上下文
- jbpm用户指南翻译:第1章 介绍
- JBPM用户指南翻译:第3章 指南
- jbpm用户指南翻译:第6章 配置
- jbpm用户指南翻译:第1章 介绍
- Jbpm用户指南翻译:第10章 上下文
- VS2010如何配置freetype2
- 从左上角飘过来的广告窗口
- 怎么样使你的虚拟主机出类拔萃?
- 生成缩略图
- 静态库、共享库、动态库的创建和使用
- RestEasy+用户指南----第5章.@PathParam
- 编译过程
- 串口操作使用初步
- 公交卡IC射频卡解密能行吗?
- INSERT , UPDATE 和 DUP_VAL_ON_INDEX异常处理
- http请求的源码
- .NET生成CSV文件
- 右下角弹窗特效
- C++中类的内存结构解析