r/SpringBoot • • Feb 25 '24

Map dto in controller

Hi my manager tell me that best practice is to map the returned dto from service layer in controller not in service Would any one agree with this ? My knowledge is the controller just one line and just delegate the logic to service layer and just return the response entity

23 Upvotes

35 comments sorted by

View all comments

1

u/DesertCookie_ Feb 26 '24

Most tutorials I've watched and read convert to DTO in the service layer. Reading through the answers here, I can see why it might be useful in some cases to only do that in the controller. Mainly, mapping to a DTO right there in the controller, possibly specifying which fields to map, makes it much easier to me to understand what's returned. I'm not a fan of having five different DTO classes for the same entity (LoginDto, RegistrationDto, PasswordChangeDto, ...) when I can also simply map only the required data and have less code that way. Of course, this inly works as long as we're talking responses. As soon as requests come into the consideration, separate DTOs is more useful again with their ability to know exactly what a client sent (and validation, ...).

But I'm a bloody beginner and hobbyist in Spring Boot. So this is really just an opinion, not an experience recommendation.