r/SpringBoot • u/Vrajesh_Darji • Jul 01 '26
Question What is good approch to make DTOs?
Recently, I have been working on my project in there in one chat controller, which has endpoints like
POST /api/chats = ChatDTO
GET /api/chats = List<ChatInfoDTO>
GET /api/chats/{chatId} = ChatDTO
PATCH /api/chats/{chatId} = ChatInfoDTO
DELETE /api/chats/{chatId} = ChatInfoDTO
POST /api/chats/{chatId}/ = ChatDTO
My question is it a good approach to use a single type of DTO for multiple endpoints, or should it be made for specific use? I have seen my friend's project for a single page, which has 8/9 DTOs. Is that normal, and the same thing ChatGPT suggests is that using multiple DTOs is a good approach.
2
u/BanaTibor Jul 04 '26
If the operations operate on the same business object then you can and actually should reuse DTOs, otherwise multiple dtos would represent the same object, causing confusion.
13
u/Signal_Help_1459 Jul 01 '26
I feel DTO can be reused, but just trying to fit everything in the same DTO is bad practise.
I have a stock market project that has well over 30+ DTOs which each serve their own purpose, from showing less data to detailed data, and so on.
9
u/repeating_bears Jul 01 '26
Reuse is fine. But in your example I have no idea what the difference between "chat" and chat info" is. I wasn't able to infer from usage.
I would expect these two endpoints to use the same DTO
GET /api/chats = List<ChatInfoDTO>
GET /api/chats/{chatId} = ChatDTO
4
u/mariusz_96 Jul 01 '26 edited Jul 01 '26
I think of them as contracts for API endpoints.
I would try to separate all requests / responses so I can edit them independently ie add data, add db JOINs.
GET / GET ALL I might reuse a response but I would make sure all requests / responses are validated / use all db JOINs.
Invalid requests / responses with missing db JOINs can really make simple things hard on client / server.
1
u/d-k-Brazz Jul 01 '26
Sharing a common DTO across multiple operations on the resource is normal
Delete should not receive or return data
What is ‘POST /api/chats/{chatId}/‘?
What are you creating here?
1
u/onated2 Jul 01 '26
AI best use case
6
u/Isogash Jul 01 '26
I prefer having canonical DTOs for each resource type because it makes fewer assumptions about what the user needs, and normally you can re-use the same behaviour on the backend, so fewer DTOs is better.
6
u/neopointer Jul 01 '26
One Suggestion I'd give is: stop calling them DTOs.
Call them ChatInfoRequest, ChatPayload and the like, for example.. It's horrible to call everything a DTO when you can find better names.
2
u/rlrutherford Senior Dev Jul 02 '26
I like immutable DTOs on the Java side, so Records are a good choice for that.
Also an aggregate of DTOs, such as your list would ideally have pagination information in it, such as page #, page size, items in page, and total items, (Not thinking about it exactly right now). So just a List<DTO> doesn’t cut it.
1
u/robinspitsandswallow Jul 02 '26
You should use multiple types but they should never be suffixed with DTO, this isn’t 2006.
0
u/MartinPeterBauer Jul 01 '26
Depends on your usecase. Who is accessing your endpoints. If its only your own app then DTOs are a waste of time. You duplicate the code for now reason.
-8
u/[deleted] Jul 01 '26
[removed] — view removed comment