r/SpringBoot • u/Jinkaza772 • Jan 04 '26
Question DTO & Entity
I have created one api endpoint for the user registration, and while I made it I found few points like in which layer sanitization should be done (client -> controller -> service -> repository -> database) then I came to know about DTO. on We can use DTO object for parsing the user request data and apply sanitization and proper checks and then we can use response DTO as well to send back the response to the client.
Well I am learning springboot, and different tutorials are doing different things, so I want to know from you guys. What should be the proper structure here and using DTO layer is really still being used in today industry ?
View all comments
Show parent comment
2
u/j4ckbauer Jan 04 '26
This sounds like my way of thinking. Unfortunately though I feel like this view is in the minority. People are horrified at the suggestion that not everything needs a DTO.
How do you address the question of 'But surely you do not intend to <gasp> expose your Entity object to the outside world?'
Everyone loves coming up with scenarios where a password or secret is added to the Entity and then unintentionally sent out on the front-end. Personally, I believe a counter-argument is that it is more dangerous to create the expectation that something is -not- automatically returned by your APIs. "Who would add a sensitive field to an Entity object without first checking where that object is used?" "Do the majority of your database entities have sensitive fields?" "Would it not suffice to use a DTO only for sensitive objects, or objects which don't easily support a 1:1 mapping between entity and request/response?"
At the end of the day you rely on validation to prevent corruption of your data, and personally I consider the blocking of sensitive fields from going out to be part of that validation. Once everything needs a DTO, you've exploded your boilerplate, and every line of boilerplate is a maintenance burden.
I'm curious how you deal with all this opposition, since if I'm understanding correctly, we may have similar views on the subject.