Quick revision
The short answer
Clients break because they parse the human message, which was never meant to be stable. I would define an error contract where every field error has a stable machine-readable code (for example the constraint name Size, or our own code such as quantity_too_large), a field path and the constraint's parameters, while the message is documented as display text that may change. The structure is written down in the OpenAPI spec and protected by contract tests, so a release cannot change it by accident.
What to remember
In this answer, we saw that clients break because they parse human messages that were never meant to be stable. The contract I would define gives every field error a stable code, a field path and the constraint parameters, and treats the message as display text that may change. The codes come from one mapping, the structure is documented in the OpenAPI spec with clear compatibility rules, contract tests assert on the stable fields and ignore the message, and the new format is rolled out next to the old one until clients have moved.
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.
Clients break because they parse the human message, which was never meant to be stable. I would define an error contract where every field error has a stable machine-readable code (for example the constraint name Size, or our own code such as quantity_too_large), a field path and the constraint's parameters, while the message is documented as display text that may change. The structure is written down in the OpenAPI spec and protected by contract tests, so a release cannot change it by accident.
Why this question is asked
Validation messages feel like a cosmetic detail until a client team builds logic on top of them. A mobile app shows a red label under the email field only if the message contains the word email, and the next Hibernate Validator upgrade changes must be a well-formed email address into something else. Nothing in our service is broken, yet the client is. Interviewers ask this design question because it tests whether we think of error responses as part of the API contract, with the same care as successful responses. In this answer, we will look at why messages drift, separate what is stable from what is not, design the error format, document it, and protect it with tests.
1Why the Messages Change in Every Release
The default messages do not come from our code. They come from the Bean Validation provider's message bundle, from Spring's own defaults, or from whichever developer last touched the annotation. Upgrading Hibernate Validator, adding a message to a properties file, fixing a typo or translating the API into a second language all change the text. Even the order of errors in a list can change, because validators do not promise an order.
So the problem is not that developers are careless. The problem is that the response gives the client only one thing to work with, the sentence, and the sentence is the one part we want to keep free to improve. The fix is to give clients something else that is designed to stay the same.
2Separating the Stable Part From the Display Part
Every field error carries three kinds of information. The first is which input failed, the field path, such as items[2].quantity. The second is what kind of failure it was, the code, such as Max or a custom quantity_too_large. The third is the human explanation, the message. The first two are facts a program can act on, so we promise to keep them stable. The third is for people, so we promise only that it is readable, not that it stays the same.
For the code, the simplest choice is the constraint name, which Spring already gives us: a FieldError for a failed @Size has the code Size as the last entry of its codes. That works well for generic rules. For business rules, a custom code says more: quantity_too_large tells the client exactly what to show, where Max only says some limit was hit. Many teams publish a short catalogue of codes and map constraint names to them in one place, so that renaming an annotation or switching from @Max to a custom constraint does not change the code the client sees. We also return the constraint's parameters, such as max: 50, so the client can build its own sentence in its own language without parsing ours.
| Field | Example | Stable? |
|---|---|---|
| field | items[2].quantity | Yes: the path in the request |
| code | quantity_too_large | Yes: only new codes are added |
| params | {"max": 50} | Yes: names fixed per code |
| message | You can order at most 50 items | No: display text, may change |
rejectedValue | 120 (omitted for secrets) | Optional, informative only |
3Designing the Error Response
We build on RFC 9457 problem details, the standard JSON error body that Spring supports through ProblemDetail. Its type field is a URI that identifies the kind of problem and is itself a stable code at the top level, for example https://api.example.com/problems/validation. We add an errors extension with our field error structure. One shared handler produces it from MethodArgumentNotValidException, HandlerMethodValidationException and our business exceptions, so every endpoint returns the same shape.
Notice that the mapping table is the only place that knows about annotation names. If a developer replaces @Max(50) with a custom @MaxQuantity constraint, we add one line to the table and the client still receives too_large. The fallback code invalid means a new rule never produces an empty code, and a test (shown below) makes sure every constraint we use has an explicit entry. The paramsOf helper reads the constraint's attributes; with Bean Validation we can unwrap the ConstraintViolation from the FieldError to get them (check your Spring version for the exact unwrap method).
4Writing the Contract Down in the API Spec
A contract that exists only in code is a contract nobody can check. We describe the problem detail and the field error as schemas in the OpenAPI document, mark code and field as required, and list the allowed codes as an enum with a sentence each. We state in the description that message is human-readable text that can change at any time and must not be parsed. We also write the compatibility rules: new codes may be added, existing codes are never renamed or removed without a deprecation period, and clients must treat an unknown code like a generic error.
That last rule matters as much as the stable codes. If clients crash on a code they do not know, we can never add a rule. A client that falls back to showing the message for unknown codes keeps working while we evolve.
5Protecting the Format With Contract Tests
Documentation drifts unless something fails when the code no longer matches it. We add tests that send a known invalid request and assert on the stable fields only, never on the message. If the team uses consumer-driven contracts, such as Pact or Spring Cloud Contract, the client teams write the expectations they rely on and our build verifies them, so a change that would break a client fails in our pipeline rather than in their app.
Notice that the test deliberately ignores the message. That is the contract in executable form: a developer can reword every message in the product, and these tests still pass, while renaming a code or moving the field fails them immediately.
6Rolling Out the Contract Without Breaking Today's Clients
The clients already parse the old format, so we cannot simply replace it. We add the new structured errors alongside the existing fields, announce the new contract, and give client teams a period, say two releases, to move over. During that period we log which clients still read the old fields if we can tell (for example, by an API version header or by client id), and we remove the old fields only when that number reaches zero. From then on, the error format follows the same versioning rules as every other part of the API.
7Follow-up questions
Should the code be the constraint name or a custom code?
Both work, and many teams use both: generic rules such as required or invalid_length map from constraint names, while business rules get explicit codes such as quantity_too_large or coupon_expired. What matters is that the mapping lives in one place and that the published list of codes is the only thing clients rely on, so annotation changes inside the service never leak out.
How do clients show messages in their own language?
They use the code and the params. A client that receives too_large with max 50 can render its own translated sentence. Alternatively, the server can localize the message from the Accept-Language header with a MessageSource, which is fine precisely because the message is not part of the stable contract.
Is changing an error code a breaking change?
Yes, and we treat it like renaming a field in a successful response. We add the new code, keep sending the old one or deprecate it with a notice, and remove it only after clients have moved. Adding a new code is not breaking, provided clients were told to handle unknown codes gracefully.
8Conclusion
In this answer, we saw that clients break because they parse human messages that were never meant to be stable. The contract I would define gives every field error a stable code, a field path and the constraint parameters, and treats the message as display text that may change. The codes come from one mapping, the structure is documented in the OpenAPI spec with clear compatibility rules, contract tests assert on the stable fields and ignore the message, and the new format is rolled out next to the old one until clients have moved.
Open with
The root cause is that our error response only gives clients a sentence, so they parse the sentence, and the sentence changes whenever someone upgrades Hibernate Validator or rewords a message.
Then, if they want more
I would split each field error into a stable part and a display part. The stable part is the field path, like items[2].quantity, a machine-readable code and the constraint parameters, such as max 50.
The code is either the constraint name, which Spring gives us on the FieldError, or a custom code for business rules, like quantity_too_large, and I map constraint names to public codes in one place so internal changes do not leak. The message stays, but it is documented as human text that may change at any time.
I would put this into an RFC 9457 problem detail with an errors extension, produced by one shared handler for all validation exceptions. Then I document it in the OpenAPI spec, list the codes as an enum, and state the rules: codes are only added, never renamed without deprecation, and clients must treat unknown codes as generic errors.
Finally, I protect it with contract tests that assert on type, field, code and params, never on the message, and ideally consumer-driven contracts from the client teams. I would roll it out alongside the old format first so nobody breaks on the switch.
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 "Clients complain that validation error messages change wording in every release and...": https://scrivio-interview-library.netlify.app/spring-boot/error-handling/0af24b2ebb/