问题
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?
e.g.
/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?
Thanks
回答1:
This is a valid @Path
:
@Path("customer/{id}") // (1)
These are not:
@Path("customer?id={id}") // (2)
@Path("customer?name={name}") // (3)
They are the same because the boil down to
@Path("customer")
which you could use.
So you can have (1)
and one of (2)
and (3)
. But you can't have (2)
and (3)
at the same time.
@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.
Edit:
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'
}
来源:https://stackoverflow.com/questions/13198043/restful-webservices-query-parameters