I think resource locators should do the job. In general, they reassign a request to another resource that is capable of consuming it.
In your case, you will have one root resource UserResource, which will process users and sub-resources for cars, messages - CarsResource, MessagesResource.
Root resource:
@Path("users") class UsersResource { // GET: /users @GET @Path("{id}") public User getById(@PathParam("id") long id) {...} @Path("{id}/cars") public CarResource getCarResource(@PathParam("id") long userId) { return new CarResource(uesrId); } @Path("{id}/sendMessage") public MessagesResource getMessagesResourceForSend(@PathParam("id") long userId) { return new MessagesResource(userId); } @Path("{id}/messages") public MessagesResource getMessagesResourceForRead(@PathParam("id") long userId) { return new MessagesResource(userId); } }
Resources for cars and messages:
class CarsResource { long userId // GET: /users/123/cars @GET public Car getAllCars() { /*retrieve all cars for user userId*/ } // GET: /users/123/cars/3 @GET @Path("{carId}") public Car getById(@PathParam("carId") carId) { /*retrieve car for id carId*/ } } class MessagesResource { long userId // POST: /users/123/sendMessage @POST public void sendMessage(@FormParam("content") String content) { /*send message to user userId*/ } // GET: /user/123/messages/123/from/123/unread @GET @Path("{id1}/from/{id2}/unread") public void getUnread(@PathParam("id1") long id1, @PathParam("id2") long id2) { /*return unread messages*/ } }
Subresources should not be annotated with @Path at the class level, and must be registered with the JAX-RS runtinme in the Application class
source share