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 ?
2
u/jfrazierjr Jan 04 '26
Think of it more like a window or view of something. DTOs generally woukd be used in MOST publicly having apis. The simplest example is a user object. You would never expose the password field to external inspection so a dto is that intermediate layer.
Spring Boot 4 also adds a proper set of view annotations so you can quickly swap the model, but I have only briefly read about them.
26
u/WaferIndependent7601 Jan 04 '26
Dto is not a layer. It’s a transport object.
You have a dto for the request. Validate it once and give it to the service layer. The service layer will also return a dto. It might be the same type or a response dto.
And yes this is still used in the industry
9
u/Difficult-Task6751 Jan 04 '26
DTO (Data Transfer Objects) are used secure the entity. If you expose the whole entity in your api call it’s not secure. So you can used the DTO’s just to expose the details you want. You don’t have to have all the fields of entity in the dto. for an example you can avoid exposing the Id of the entity.
And yes, this is been widely in use in industry.
3
u/dotceng Jan 04 '26
DTO's are objects for API's data flows. For example;
You write Login API, think about it a bit you must receive user's email(or username) and password to authorize them. So you must create DTO (LoginRequestDto.java) and include email and password fields.
'@Data' // -> coming from lombok, using for set getters and setters for private fields;
public class LoginRequestDto {
private String email;
private String password;
}
And in a controller; public ResponseEntity<LoginResponseDto> login(@RequestBody LoginRequestDto request) { }
So you can see, DTO's duty is transmit datas from FE to BE or BE to FE. So, We must write LoginResponseDto for result.
In example; If its success to authorize user, the API return to client Token or User Info. We must indicate this fields in LoginResponseDto.java.
And finally, DTO is not a layer, these are actually classes for provide datas between FE and BE.
4
u/bikeram Jan 04 '26
Yes they’re definitely still used. But think of them as an interface. Don’t start creating them because it “might” be useful in the future. Add them as the situation arises.
Depending on project size, I’ll create a package under my entity package and call it dto. I try to use very clear naming.
It’s very easy to lose track of what you’ve created and actually used in your frontend.
1
u/Ali_Ben_Amor999 Jan 05 '26
In JPA, once you annotate a class with @Entity it becomes a special unpredictable object be it lazy loading issues in the wrong place, active dirty checking in case you innocently tried to pre format a field to display but its now persisted, and many more scenarios.
To limit this uncontrolled behaviour devs use an intermediate class that contain the data without unpredictable behavior. The known term is DTO I prefer calling it entity view. The dto class is a simple java object without any proxy controlling it which make working with it safer internally and when exposed.
What I recommend, is to create classes for request payloads that modify an entity separately don't pass entities as input, use JSR-380 annotations to validate data then use a library like mapstruct to map the dto into an entity you can sanitize your data further more at service level then persist.
For response use spring data jpa Projections for safer entity views.
Once you grasp JPA take a look at Blaze Persistence its an amazing library that makes using DTOs for direct insert/update easy and predictable
1
u/Mindless_Security744 Jan 06 '26
In short I use DTOs when I transform or enhance data. Between my APIs I have no problem speaking DAO to DAO but if I modify it or transform it, then DTO. Easy.
1
u/Hour-Night8287 Jan 06 '26
DTO can prevent you api to prevent the entire entity object, after successfully registration , you can return only the token, username, userId, email or other relevant data, and not exposing passwords
1
u/themasterengineeer Jan 06 '26
In short, DTO is for anything coming in or going out of your REST API, here’s an example on how to use DTOs:
https://youtu.be/a9pCWJF_LiA
1
u/devmoosun Jan 08 '26
I use DTO in all of my projects. You don’t want to expose unnecessary response to the client.
1
u/Physical-Silver-9214 Jan 09 '26
There's JsonProperties where your can make fields write only to prevent it from being displayed when is passed to a controller. Yes DTO'S can save you from mistakenly exporting your data. But you can always have something from the entity player help reduce the probability of letting it get out.