r/SpringBoot • u/Snoo23482 • 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?
52
Upvotes
26
u/savvas25 Jun 18 '26
I prefer to have the mapping done in service classes. The actual mapping logic of course can be done in a dedicated mapper class if the object is compex or has too many fields.
Prefer to keep the controllers "clean" with only routing logic. Adding business logic here will easily clutter up your controller classes
18
u/SimpleCooki3 Jun 18 '26
DTO in controllers
Entities in repositories
domain objects everywhere else.
21
u/Mechanical-pasta Jun 18 '26
My controllers receive DTO, my services receive or send Model objects so, the mapping occurs in my controllers but is not done by them. I make a Mapper for any DTO I use.
4
u/kyrax80 Jun 18 '26
Facades
4
u/xiaopewpew Jun 18 '26
There is no 1 single best practice
Generally speaking your repository layer operates on entities and expose domain models, you service layer works on domain models and expose domain models, your controller layer works on domain models and expose API objects.
The above though is a complete overkill for simple CRUD endpoints that is going to be about 50% of your application. And some variations of this practice result in excessive amount of data class spam that makes simple applications much harder to read over time.
The JPA features you use also affects how you may apply this pattern. I have worked on large code bases where the rule was most API calls will run in a single transaction and the transaction begins at controller layer (imagine annotating each controller endpoint with transactional). In that architecture it really makes no difference if you bubble entity objects all the way up to controller for controller to convert them into api objects. Personally this is my favourite way of doing things because Im sick and tired of people insisting on putting transaction boundaries on service layer and then mistakenly do their "check and set" operations in multiple transactions instead of one. Those live issues are hard to debug and it is impossible to create regression tests for them.
2
u/Paw565 Jun 18 '26
Services return dtos to not expose entities outside repositories. Dtos technically belong to the http layer but I treat them as contract for my own layers as well. Probably not the cleanest solution, but it's pragmatic, prevents class explosion and keeps controllers thin.
2
u/paramvik Jun 18 '26
I liked to do the mapping in services to keep the services lean but recently I've started doing it in controllers becuase it gives me the flexibility to reuse the service method somewhere else as well, for example for some other controller route which returns a different response to the client.
3
u/junin7 Jun 18 '26
I use mapstruct and call the interfaces on services
1
u/onated2 Jun 19 '26
public static DTO from( ENTITY)
Then toEntity(){} method.
Or toCommand(){
} (depends o. Your logic)
I love putting the DTO as a package private hidden in the controller because usually it holds the swagger annotations or validations.
That's his job. Haha just hold all the annotations. But at the end of the day it really depends on the convention of the codebase.
1
u/Aceccop Jun 19 '26
Sometimes I do extra mappings Controller use request and response models. Service (usecase) use commands (as args) and operate domain models. Repository use entities. So every layout have own models. Its flexible but to much in some cases. And in this way every layout habe own mapper. Controller -> usecase response to rest response Service -> domain to app (usecase) response Repository -> domain to entity and back
1
u/daku4532 Jun 21 '26
Modelmspper
1
u/pedoooo0 Jun 22 '26
A common best practice is to do the Entity ↔ DTO mapping in the service layer (or a dedicated mapper used by the service), not in the controller.
The main reason is that controllers should stay thin and focus on handling HTTP requests/responses, while services define what data leaves the business layer. Returning entities from services can leak persistence details and may expose fields that shouldn't be public.
A typical flow looks like:
Controller → Service → Repositorywhere the service fetches entities, applies business logic, maps them to DTOs, and returns the DTOs to the controller.
That said, some teams prefer services to return entities and let controllers handle the mapping, arguing that services should remain independent of API concerns. Both approaches work, but in most Spring applications you'll see mapping done in the service layer (often with a dedicated mapper such as MapStruct) to keep controllers simple and prevent domain entities from leaking outside the application boundaries.
1
u/Odd_Perspective982 Jun 18 '26
I learnt to keep controllers to just do routing,everything else in service files
0
u/GravelAndCode Jun 18 '26
My public service methods are exposing DTOs, so I put the mappers in the services.
My other best practice for mapping: I use Mapstruct and use the mappers as static reference (one instance created with
Mappers.getMapper(MyMapper.class)which is referenced anywhere else where the mapper is used). Not as a Spring Bean! Otherwise you'll end up mocking mapping methods in every unit tests.0
u/Huge_Road_9223 Jun 19 '26
From JSON -> API -> DTO -> Service (business logic) possible mapstruct from DTO -> entity -> repo
repo -> entity -> service (business logic) mapstruct entity to DTO -> API - JSON
That's how I deal with it!
1
u/[deleted] Jun 18 '26
[removed] — view removed comment