RESTful Web服务:查询参数 [英] RESTful webservices: query parameters
问题描述
我正在将JAX-RS与RESTEasy一起使用.
I am using JAX-RS with RESTEasy.
我想知道我们是否可以代表不同的资源,而其路径仅通过查询参数的顺序和数量来区分?
I want to know if we can have represent different resources with path differentiated only by order and number of query parameters?
例如
/customer/1234
/customer?id=1234
/customer?name=James
我可以创建三种不同的方法吗?
Can I create three different methods e.g.
@Path("customer/{id}")
public Response get(@PathParam("id") final Long id) {
..
}
@Path("customer?id={id}")
public Response get(@QueryParam("id") final Long id) {
..
}
@Path("customer?name={name}")
public Response get(@QueryParam("name") final String name) {
..
}
这项工作是否可以通过区分这样的路径来调用不同的方法?
Will this work, can I invoke different methods by differentiating path like this?
谢谢
推荐答案
这是有效的@Path
:
@Path("customer/{id}") // (1)
这些不是:
@Path("customer?id={id}") // (2)
@Path("customer?name={name}") // (3)
它们是相同的,因为归结为
They are the same because the boil down to
@Path("customer")
您可以使用的
因此您可以拥有(1)
以及(2)
和(3)
之一.但是您不能同时拥有(2)
和(3)
.
So you can have (1)
and one of (2)
and (3)
. But you can't have (2)
and (3)
at the same time.
@QueryParam
参数是@Path
的 not 部分.您可以像在方法签名中一样访问,但是您不能基于它们来进行JAX-RS的路由.
@QueryParam
parameters are not part of the @Path
. You can access them as you do in the signature of the method but you can't base the routing of JAX-RS on them.
您可以编写 one 方法,该方法将id
和name
都接受为@QueryParam
.这些查询参数是可选的.
You can write one method that accepts both id
and name
as @QueryParam
. These query parameters are optional.
@Path("customer")
public Response get(@QueryParam("id") final String id,
@QueryParam("name") final String name) {
// Look up the Customers based on 'id' and/or 'name'
}
这篇关于RESTful Web服务:查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!