Spring REST loading route mapping

I just think that it is best to create PATH mapping for the leisure service. Let them say that we have the following ways:

/users POST /users/1 PATCH, GET /users/1/contacts GET, POST /users/1/contacts/1 GET, PATCH 

The question is what is the best way to create controllers. For example, we have a UserController where we can technically place all these mappings. Or - we must create separate controllers (UserController, ContactsController). fe UserController below if we all put it.

 @RequestMapping("users") @RestController public class UserController { @RequestMapping(method = RequestMethod.POST) public ResponseEntity<Void> createUser() {} @RequestMapping(method = RequestMethod.GET) public User getUser() {} @RequestMapping(value = "{id}/contacts", method = RequestMethod.GET) public List<Contact> getContacts() {} @RequestMapping(value = "{id}/contacts", method = RequestMethod.POST) public ResponseEntity<Void> createContact() {} ..... } 

And if we create separate controllers, then how should the paths be organized? This is probably a stupid question, but I would be glad if someone could share the experience.

+5
source share
2 answers

Let's assume that the number of objects associated with the user will increase in the future. Therefore, it is obvious that it is better to break it into objects:

UserController -> UserService -> UserRepository,

ContactController β†’ ContactService β†’ ContactRepository,

FriendshipController β†’ FriendshipService β†’ FriendshipRepository

From my experience, User Controller

 @RestController @RequestMapping("/user") public class UserController extends AbstractController { ... @RequestMapping(method = RequestMethod.POST) public ResponseEntity<?> createUser(@RequestHeader("X-Auth-Token") Optional<String> @RequestBody User user) { ... @RequestMapping(method = RequestMethod.GET) public ResponseEntity<?> listUsers(@RequestHeader("X-Auth-Token") Optional<String> authToken) { ... 

associated with the user area. Friendship Controller:

 @RestController @RequestMapping("/user/{id}") public class FriendshipController extends AbstractController { ... @RequestMapping(value = "/friendship/code", method = RequestMethod.POST) public ResponseEntity<?> generateCodeForUser(@PathVariable("id") long id) { ... @RequestMapping(value = "/friendship/code", method = RequestMethod.GET) public ResponseEntity<?> retrieveCodeForUser(@PathVariable("id") long id) { ... 

Not sure if this is an axiom, but help me organize my code.

+3
source

I am a fan for individual controllers, as they remove the connection between Users and Contacts (for example) - what if later you want to use Contacts in a separate context? (i.e. regardless of User they belong)

If they are separated, the paths will look very similar to what you have:

Users

 /users GET, POST /users/{user-id} PATCH, GET 

Contacts

 /contacts GET, POST /contacts/{contact-id} GET, PATCH 

If there is a relationship between Contacts and Users (and in this case, it looks like it is), you can have it like this:

 /contacts/for-user/{user-id} GET, POST /contacts/for-user/{user-id}/{contact-id} GET, PATCH 

This allows you to add contacts to other things (and not just to users) and to other contexts (regardless of their contacts).

For example, coffee makers

 /coffee-maker/users GET 

Or if the coffee maker fails, who will we contact for repairs?

 /coffee-maker/contacts GET 

Perhaps the contact for repairing the coffee machine is also a user (but they are not necessary)

One more thing, you can rotate it and say that the contact is for the device (i.e. coffee maker)

  /contact/for-appliance/{appliance-id} GET 

My point is that if you separate contacts from users, you can assign contacts to other objects instead of formatting the relationship User -> Contacts - unless, of course, you want to separate them due to business rules or according to the reason

In any case, beware that there are many ways to make a comparison.

+1
source

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


All Articles