swch
swch

Reputation: 1546

Rest controllers for relation entities conventions

I have simple java app with three entities:

User
Rent
Tool

User can rent multiple tools and tool can be rent by multiple users. The Rent entity is there to track rent history so it has many to one relations with both User and Tool.

At the moment I have two rest controllers: UserController ToolController that are responsible for serving users or tools.

The problem is that now i want to return rent history for specific user. Should I create method in UserController with mapping like

/users/{id}/rents

or it is better to create separate controller like RentController and method with mapping like

/rents?userId={userId}

I think that first approach is cleaner in terms of URI, but I have to use the RentService in UserController and because these are classes from different packages (I use package-by-feature approach) I feel that there is something wrong. The problem is also with methods like POST, or PUT, where I think the best option will be something like this:

POST /rents - RentDto as body
PUT /rents/{id} - RentDto as body

or maybe it is ok to have method:

GET /users/{id}/rents

in RentController and not in UserController? How you solve such problems and what would you suggest?

Upvotes: 0

Views: 265

Answers (2)

Eduardo Meneses
Eduardo Meneses

Reputation: 524

When we are talking about RESTful API we should be able to reach any CRUD method without extra parameters, so, considering that, I would use the path parameter approach (/users/{id}/rents).

Normally we use query parameters in case that you want to get a list or items of a list with a very specific information.

I recommend this article for more details.

Upvotes: 3

Huy Nguyen
Huy Nguyen

Reputation: 2061

/users/{id}/rents

About my opinion, it's more clearly and easy to understand if you use above url and just return the list rents of user.

/rents/{id}/user

Will help to track this rent is currently used by who.

Upvotes: 0

Related Questions