JAX-RS @QueryParam和 @MatrixParam?之间的区别是什么?从文档中,queryparam和matrixparam都可以在特殊条件下定位一个资源.那么用例区别是什么?
PS:
Queryparam:
url ? key=value;
Matrixparam
url; key=value;
and*_*dih 14
在
@PathParam和其他基于参数的注解,@MatrixParam,@HeaderParam,@CookieParam,@FormParam遵守同样的规则@QueryParam.@MatrixParam从URL路径段中提取信息.@HeaderParam从HTTP标头中提取信息.@CookieParam从与cookie相关的HTTP标头中声明的cookie中提取信息.
示例(从这里得出):
@Path("/books")
public class BookService {
@GET
@Path("{year}")
public Response getBooks(@PathParam("year") String year,
@MatrixParam("author") String author,
@MatrixParam("country") String country) {
return Response
.status(200)
.entity("getBooks is called, year : " + year
+ ", author : " + author + ", country : " + country)
.build();
}
}
Run Code Online (Sandbox Code Playgroud)
请参阅以下URI模式和结果:
URI模式:"/ books/2012 /"
getBooks被调用,年份:2012年,作者:null,国家:null
URI模式:"/ books/2012; author = andih"
getBooks被称为,年份:2012年,作者:andih,国家:null
URI模式:"/ books/2012; author = andih; country = germany"
getBooks被称为,年份:2012年,作者:andih,国家:德国
URI模式:"/ books/2012; country = germany; author = andih"
getBooks被称为,年份:2012年,作者:andih,国家:德国
有关差异的解释,您可以查看 URL矩阵参数与请求参数
Har*_*tel 14
该
@MatrixParam注释将适用于特定的资源存在于URL和@QueryParam将适用于整个请求URL.
举一个超市的例子,如果你想要满足多种条件的所有水果,比如type = fruits,价格范围从300开始,列出匹配的10个水果,你可以去下面的API Design,
http://dev.brandstore.com/inventory/grocery;type=fruits/price;range=300/?limit=10
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,第一个Matrix Param type=fruits仅应用于杂货资源,同样range=300仅适用于价格资源,但分页的查询参数limit=10适用于整个请求URL.是的,如果只使用查询参数,你最终会得到像"grocery_type"和"grocery_price"这样的参数,你会失去请求中参数局部性所增加的清晰度.
| 归档时间: |
|
| 查看次数: |
29557 次 |
| 最近记录: |