Nested resources in Jersey / JAX -RS -how to implement the example using the example

Thus, in reality the invested resources cannot be named from the perspective of β€œRest”, but I am interested in how to structure the Jersey class as a rest provider so that it can respond to chained requests.

those. I'm fine with the main / users, I'm fine with / users / 123 to get a specific user, but how then to go to the user properties .... / users / 123 / cars, / users / 123 / cars / 23 and so on .d.

Sorry for the lack of information, but saw this as an example in the documentation for the restant for Angular.

https://github.com/mgonto/restangular#production-apps-using-

restangular // Restangular returns promises Restangular.all('users').getList() // GET: /users .then(function(users) { // returns a list of users $scope.user = users[0]; // first Restangular obj in list: { id: 123 } }) // Later in the code... // Restangular objects are self-aware and know how to make their own RESTful requests $scope.user.getList('cars'); // GET: /users/123/cars // You can also use your own custom methods on Restangular objects $scope.user.sendMessage(); // POST: /users/123/sendMessage // Chain methods together to easily build complex requests $scope.user.one('messages', 123).one('from', 123).getList('unread'); // GET: /user/123/messages/123/from/123/unread 
+6
source share
2 answers

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

+8
source

in addition to Thomas Bartalos answer, you can use the path parameter identifier in the sub-resources

 @GET @Path("{id1}/from/{id2}/unread") public void getUnread(@PathParam("id") long userId,@PathParam("id1") long id1, @PathParam("id2") long id2) { /*return unread messages for user with userId*/ } 

this is useful if you use statless beans, it avoids passing the userId parameter during instanciation.

Example: root resource:

 @Path("users") @Stateless class UsersResource { @Inject CarResource cr; @Inject MessageResource mr; // GET: /users @GET @Path("{id}") public User getById(@PathParam("id") long id) {...} @Path("{userId}/cars") public CarResource getCarResource() { return cr; } @Path("{userId}/sendMessage") public MessagesResource getMessagesResourceForSend() { return mr; } @Path("{userId}/messages") public MessagesResource getMessagesResourceForRead() { return mr; } } 

auxiliary resources:

 @Stateless @Path("/") class CarsResource { @GET public Car getAllCars(@PathParam("userId") long userId) {//the path param is retrieved from parent path /*retrieve all cars for user userId*/ } @GET @Path("{carId}") public Car getById(@PathParam("userId") long userId,@PathParam("carId") carId) { /*retrieve car for id carId fr the user with userId*/ } } @Stateless @Path("/") class MessagesResource { @POST public void sendMessage(@PathParam("userId") long userId,@FormParam("content") String content) { /*send message to user userId*/ } @GET @Path("{id1}/from/{id2}/unread") public void getUnread(@PathParam("userId") long userId,@PathParam("id1") long id1, @PathParam("id2") long id2) { /*return unread messages*/ } } 
0
source

Source: https://habr.com/ru/post/975045/


All Articles