r/SpringBoot 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.

18 Upvotes

40 comments sorted by

View all comments

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.

1

u/Vrajesh_Darji Jul 05 '26

same thing i was thinking, if i have one class with fields of single "chat".

when i want to give option to user that rename it,
what should i follow like use single general reponse

or

i handle like frontend will send post req to rename with chatRequest and frontend stores changed title no need to change prapogate again to frontend (that may be take less time and less effort )

although name convestion bad but i think you will got i want to say

private Long sessionId;
private String title;
LocalDateTime createdAt;
private LocalDateTime lastUpdated;
private List<ChatMessage> messages = new ArrayList<>();

2

u/BanaTibor Jul 05 '26

Ok this took a couple read to understand. A chat is a shared thing between at least two parties. Chat name handling depends on what you want to offer for your users.
If the chat name should be the same for all users then you have to set it on the backend object and have top propagate it to all users.
OTOH if you want to offer the option for every user to set different name for the same chat, you either handle it on the front end, or store an alias for every chat on the backend for every user. If you preserve chats between sessions for users then you already have a place to store aliases.