Quick revision
The short answer
I would replace the groups with separate DTOs per operation, such as CreateOrderRequest, UpdateOrderRequest and CancelOrderRequest, each with plain constraints that apply every time. Groups are a good tool only for small differences, like an id that is null on create and required on update. Rules shared by several DTOs go into composed annotations, so they are written once without bringing the groups back.
What to remember
In this answer, we saw why six validation groups on one DTO hide which rules apply, replaced them with one request DTO per operation such as CreateOrderRequest and UpdateOrderRequest with plain constraints, kept shared rules in composed annotations, limited groups to small differences such as the id rule, and migrated one endpoint at a time behind tests that pin the current behaviour.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would replace the groups with separate DTOs per operation, such as CreateOrderRequest, UpdateOrderRequest and CancelOrderRequest, each with plain constraints that apply every time. Groups are a good tool only for small differences, like an id that is null on create and required on update. Rules shared by several DTOs go into composed annotations, so they are written once without bringing the groups back.
Background to this question
Validation groups start innocently: one group for create, one for update. Then come a draft state, an admin override, an import path and a partial update, and one DTO carries six groups. Every field has several annotations with different group lists, each endpoint names a different combination, and nobody can answer the simple question of which rules apply to this request without reading the whole class. In this answer, we will look at why groups scale badly, split the DTO into one class per operation, keep shared rules in composed annotations, and plan the migration safely.
1Why Six Groups Become Unreadable
A group is a label on a constraint, and each validation run checks only the labels it names. With two groups, a reader can keep both in mind. With six, the rules for any one operation are scattered: to know what applies to an admin update, we must read every annotation on every field, filter by group, remember which groups extend Default, and check the group list on the endpoint. The class no longer describes any single request.
Notice the other cost: the DTO advertises fields that make no sense for some operations, such as cancelReason on create. Generated API documentation shows one schema with every field optional, because the documentation tool cannot express this group applies here, so clients cannot learn the rules from the docs either.
2One DTO per Operation
The clearer design is a request class per operation. Each one contains only the fields that operation accepts and only the constraints that always apply to it. Records make this cheap.
| Question | One DTO with six groups | One DTO per operation |
|---|---|---|
| Which rules apply to cancel? | read all fields, filter by group | read CancelOrderRequest |
| Fields a client may send | every field, always | only that operation's fields |
| API documentation | one vague schema | a precise schema per operation |
| Risk of the Default group trap | high | none, no groups |
| Number of classes | one | one per operation |
Now the question what is checked on cancel has a one-line answer: read CancelOrderRequest. The id problem disappears because update and cancel take the id from the path, so no DTO needs @Null and @NotNull on the same field. Plain @Valid works everywhere again, the documentation shows a precise schema per operation, and an unknown field such as cancelReason on create can be rejected by Jackson instead of silently ignored. The cost is more classes and some mapping code into the domain, which is small and easy to test.
3Sharing Rules With Composed Annotations
Splitting DTOs raises a fair worry: the lines rule, not empty and at most 50, now appears twice. If it changes, someone might update only one copy. Bean Validation has an answer for this: a composed constraint, an annotation that bundles other constraints under one name.
The rule now has a name and one definition, and it reads like domain language. If we prefer a single message instead of one per inner constraint, adding @ReportAsSingleViolation makes the composed annotation report only its own message.
4Where Groups Still Fit
Groups are not bad; they are a tool for small differences. If two operations differ in exactly one rule, such as the id being null on create and required on update, a single DTO with two groups that extend Default is simpler than two nearly identical classes. A useful rule of thumb for a team guideline is: at most two groups on a DTO, and as soon as a third would be needed, or a field makes sense for only some operations, split the DTO.
5Migrating Without Breaking Clients
The new DTOs must accept exactly what the old one accepted for each operation, or clients break. So before changing anything, we write tests per endpoint that send valid and invalid requests and record the current status codes and messages. Then we introduce the new DTO for one endpoint at a time, run the same tests, and remove the group annotations that no longer have a user. If the old DTO accepted fields it ignored, we decide deliberately whether the new one should ignore or reject them, and announce a stricter behaviour as a change. After the last endpoint moves, the group interfaces can be deleted.
- Write tests per endpointrecord current valid and invalid behaviour
- Create the DTO for one operationonly its fields and rules
- Switch the endpoint to @Validmap the DTO to the domain
- Do the tests still pass?YesNext endpointNoFix the DTO, or document a deliberate change
- Delete unused groupsafter the last endpoint
5 steps in order. Play them, or step through them one at a time.
6Follow-up questions
Does one DTO per operation not duplicate a lot of code?
Some, but the duplication is in field declarations, which are cheap and explicit, while the rules themselves can be shared through composed annotations and shared nested types such as LineDto. In exchange, every class can be understood on its own, which matters more as the API grows.
Should the response DTO also be split?
Not necessarily. Responses do not need validation, and one OrderDto for output is usually fine. The splitting is about input, where each operation genuinely accepts different data.
Could a rules engine or the service replace these annotations?
For business rules that depend on state, such as whether an order can still be cancelled, yes, that belongs in the service. But field shape rules like not empty and at most 50 lines are exactly what annotations on a request DTO do best, so they stay there.
7Conclusion
In this answer, we saw why six validation groups on one DTO hide which rules apply, replaced them with one request DTO per operation such as CreateOrderRequest and UpdateOrderRequest with plain constraints, kept shared rules in composed annotations, limited groups to small differences such as the id rule, and migrated one endpoint at a time behind tests that pin the current behaviour.
Open with
Six groups on one DTO is a sign the class is trying to describe six different requests at once. To know which rules apply to one operation, you have to read every annotation and filter by group, and the API docs show one vague schema where everything looks optional.
Then, if they want more
So I would split it into one request DTO per operation, like CreateOrderRequest, UpdateOrderRequest and CancelOrderRequest. Each one has only the fields that operation accepts and plain constraints that always apply, so we are back to simple @Valid, there is no Default group trap, and the docs become precise.
Many group rules disappear on their own, for example the id moves to the path for update and cancel. For rules shared between DTOs, like the order lines being non-empty and at most 50, I would write a composed annotation, @OrderLines, that bundles @NotEmpty and @Size, so the rule has one definition and a domain name.
Groups still have a place for small differences, like an id that is null on create and required on update, and I would set a guideline of at most two. To migrate safely, I would first write tests that pin each endpoint's current behaviour, then move one endpoint at a time.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Validation groups have grown to six groups on one DTO and nobody understands which rules...": https://scrivio-interview-library.netlify.app/spring-boot/request-validation/6bf83c4b74/