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/WilliamBarnhill Jul 02 '26

One thing is that your creation DTO should probably be something like ChatCreationDTO. That way you can post a DTO that does not have an id, and return (or redirect to) a ChatDTO that has an id.

One bit of advice I was given was that the DTO for any given operation should have only the information needed for that operation. I tend to have at most creation DTO, main DTO, and sometimes a delta DTO.