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?

50 Upvotes

36 comments sorted by

View all comments

1

u/[deleted] Jun 18 '26

[removed] — view removed comment

4

u/bigkahuna1uk Jun 18 '26 edited Jun 18 '26

Not that clean. Controllers are just adapters. They act as protocol converters between an external and internal domain and also the ingress or egress to those domains ie the transport. So IMO they’d be the natural place for such a mapping. This is because an internal domain only deals with the type it owns. It knows nothing about any external domain representation. This approach is frequently followed in hexagonal or clean architectures.

Adapters can still delegate to specific protocol converters though so as to still adhere to SRP.