0

From the perpective of Restful Apis, its said its a good choice to design them hierarchical when your database is hierarchical too, above all because the client learns and knows the hierarchical structure of the entities. I mean, for instance, if you have bank clients and accounts, the parent entity would be the clients and the child entities would be the accounts. So :

  • To get the accounts from the client 1, a right URI could be something like "/clients/1/accounts"

From the perspective of Spring controllers, I should have a ClientController and a AccountController but

  1. The AccountController should process the above request, right?

  2. Could I specify and URI like "accounts/?clientId=1"? Its a bad design?

  3. If I go with the option 1, how to specify this URI in the AccountsController?? If not, should I create another controller just for this and not put this URI in the Account controller?

     @RequestMapping("/clients")
     public class ClientsController{ }
    
    
     @RequestMapping("/accounts")
     public class AccountsController{
    
        @RequestMapping("/clients/{idClient}/accounts") => **I cannot specify 
       an absolute path here because 
        its relative to the RequestMapping annotation in the Controller**
       @GetMapping   
        public @ResponseBody List<Account> getAccounts(){}
    
    
     }
    

Thanks!!

1 Answer 1

1

There's no hard bound rules it's matter of choice and use cases that decides how we structure our rest uris and using query param or path variables is for constructing meaningful and readable uris.

Suppose if you have.usecase is like to get the list of accounts needs a client ID as mandatory then construct:

GET /clients/{id}/accounts And put it in clients controller.

But if your usecase is like client id is not mandatory to get list of accounts then construct: GET /accounts?clientid=1 And put it in accounts controller. Now you can choose to make clientid as required=false request param.

Don't construct deeply nested APIs.. make dumb endpoints eventually you'll end up facing usecases where you'll need to create non nested uris.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.