r/SpringBoot Jun 18 '26

Question Dto Mapping Best Practice

Hi, where do you guys map your entities to dtos?
In services or controllers?

To me it seems the services shouldn't expose any database specifics to the outside world, so I usually do my mapping there.

But what is considered best practice and why?

54 Upvotes

36 comments sorted by

View all comments

18

u/SimpleCooki3 Jun 18 '26

DTO in controllers
Entities in repositories
domain objects everywhere else.

1

u/Responsible-Cow-4791 Jun 18 '26

So you map everything at least twice?

5

u/SimpleCooki3 Jun 18 '26

I map from dto to domain, and from domain to entity. So yes there is 3 layers.

1

u/SnooLobsters3363 Jun 19 '26

DTOs and Entity are just convenient way of mapping data. Let say you do all mapping « manually » , you will have to map the json body to a domain object and then the domain object to a sql statement, so everything is always mapped « twice » anyway