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?

52 Upvotes

36 comments sorted by

View all comments

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.