您的位置:首页 > 其它

rest @PathParam

2016-07-14 10:30 281 查看
@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 方法

 

<span style="font-size: 16px;">  public class ISBN {

public static ISBN valueOf(String isbn) {...}
}</span>

在@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
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: