Suppose I have a set of JAX-RS locators and sublocators, like the following:
@Path("/users")
public class UserListResource {
@Path("/{id}")
public UserResource getCustomer(@PathParam("id") int id) {
// Find and return user object
}
}
public class UserResource {
@GET
public String get() {...}
}
For example, a UserResource
object with the ID 5
would have the path "/users/5"
. In my system, I have several different resources.
Now the question is: How can the server figure out the path of a given resource? Can I do this programmatically via some JAX-RS API or do I have to implement code that uses reflection? (I know how to do the latter, but would prefer the other approach.)
UriInfo
object that provides this, but I need to know the path in advance (to inform clients of a change that did not necessarily happen through the JAX-RS resource)."/users"
and "/{id}"
).As I read your question, you need to build a URI knowing only the resource class and the id
parameter.
It can be done using the UriBuilder
class as in:
UriBuilder builder=UriBuilder.fromResource(UserListResource.class);
URI uri=builder.path(UserListResource.class,"getCustomer").build(5);
It uses reflection under the hood, so it is not so easy to refactor, but it is all it is available at the moment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With