In general, keep in mind that something sounds rather strange with the architecture of your application. It's hard to put your finger on, but the pattern of questions you ask raises a series of red flags about how you are going to do it. Keep in mind that if you want to create a RESTful API for your application that you might need to stop, take a few steps back and rethink what you are trying to do.
To your obvious questions ...
Now the question is: how can the server determine the path to a given resource? Can I do this programmatically through some JAX-RS API or do I need to implement code that uses reflection? (I know how to do the latter, but would prefer a different approach.)
The server knows the path that is always provided by the user and is used to navigate the collection of resource classes that make up your application. If you need an instance of UriInfo for a specific call, you must enter it as part of that specific call:
@GET public String get(@Context UriInfo info) {...}
Any information required from an external context (for example, what is a resource identifier) is best conveyed during construction. You can re-view it from the URL (which can be obtained from UriInfo ), but this is probably the wrong approach.
Otherwise, if you are doing something much more complex, then you need to be more specific in your question.
- At that moment when I need to know the path, I do not have a request object at all. For example, I have a timer that does some background processing and then changes some objects in the domain model and then informs all clients about the changed entities (including their paths).
- I know that as part of the request, I can add a
UriInfo object that provides this, but I need to know the path in advance (to inform clients about changes that have not necessarily occurred through JAX-RS).
How are you going to inform customers? Usually there is no mechanism for sending messages from the server to clients, and clients are usually protected from the firewall, so they cannot host the service directly.
Theoretically, you can link (explicitly, by URL) each resource with its own RSS feed, which the client could listen to if they selected. You cannot make customers listen, but you can give them the opportunity to do so. If you go along this route, you don’t need to know UriInfo “ahead of time”, because the location information will be present at key points (i.e., when creating the resource), and then you simply refer to the fact that you have control.
But this is only one way to do this, and it adds a lot of complexity; you would only do this if it was important to your application. It is often easier to just do a customer survey from time to time. (Note that some types of modifications are inherently very destructive, especially by changing the identifier or deleting a resource. Do not expect something to smoothly deal with them.)
- I do not want to repeat the path information elsewhere, and I also do not want to have a set of path fragment constants for each resource type (in this case, “
/users ” and “ /{id} ”).
Tough. Repeated information in several places, if you are conducting it sequentially from the source of one , is common practice. There is nothing wrong.