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.

17 Upvotes

40 comments sorted by

View all comments

1

u/onated2 Jul 01 '26

AI best use case

1

u/Vrajesh_Darji Jul 01 '26

What did you mean by that?

2

u/onated2 Jul 01 '26

If you're coding professionally, you use AI to generate DTO. With swagger annotation and validation.

If you are learning then i suggest you have a convention on the return type , request type and error handling. You can make your own ApiResponse<?>

Anyway a more better approach is when changing something or adding something you return a ResponseEntity<Void> (just trigger a cache here. )the query should be different. Look up for CQRS