RESTful Web服务:查询参数

RESTful Web服务:查询参数,rest,path,resteasy,Rest,Path,Resteasy,我将JAX-RS与RESTEasy结合使用 我想知道的是,我们是否可以用路径表示不同的资源,只根据查询参数的顺序和数量进行区分 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) { ..

我将JAX-RS与RESTEasy结合使用

我想知道的是,我们是否可以用路径表示不同的资源,只根据查询参数的顺序和数量进行区分

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) {
..
}
这行得通吗?我可以通过这样区分路径来调用不同的方法吗


谢谢

这是一个有效的
@路径

@Path("customer/{id}")        // (1)
这些不是:

@Path("customer?id={id}")     // (2)
@Path("customer?name={name}") // (3)
它们是一样的,因为它们归结为

@Path("customer")
你可以用它

所以你可以有
(1)
(2)
(3)
中的一个。但是你不能同时拥有
(2)
(3)

@QueryParam
参数不是
@Path
的一部分。您可以像在方法签名中那样访问它们,但不能基于它们来进行JAX-RS路由

编辑:

您可以编写一个同时接受
id
name
作为
@QueryParam
的方法。这些查询参数是可选的

@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'
}
@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'
}