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
1
u/Ali_Ben_Amor999 Jan 04 '26
If you are worried about CPU waste, you should consider dropping JPA in the first place because the jpa provider is doing too much which make copying 5 or 20 fields negligible. While personally I don't see issue returning the entity with some Jackson view annotations if the entity is simple. But this can be prone to transactional context issues like transactional life cycles which require an open session even at controller level also it can make project inconsistent when you have an endpoint returning a POJO and another returning an entity but it all depends on the use case