Quick revision
The short answer
The standard says that input is validated at the API boundary with Bean Validation on request DTOs, that every service returns one error format (ProblemDetail with an errors list holding a field path, a stable code and a message), and that common rules such as currency or country codes come from a shared constraints library. Business rules live in the service layer and fail with typed errors, and every rule is covered by tests and reflected in the OpenAPI spec.
What to remember
In this answer, we defined a validation standard. Input is validated at the boundary with Bean Validation on request DTOs, errors use one ProblemDetail format with field paths, stable codes and messages, common rules come from a shared constraints library that also ships the error advice, business rules live in services and throw typed errors with codes, and tests plus a generated, linted OpenAPI spec keep the rules and the documentation in step.
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.
The standard says that input is validated at the API boundary with Bean Validation on request DTOs, that every service returns one error format (ProblemDetail with an errors list holding a field path, a stable code and a message), and that common rules such as currency or country codes come from a shared constraints library. Business rules live in the service layer and fail with typed errors, and every rule is covered by tests and reflected in the OpenAPI spec.
Why this question is asked
Without a standard, each team answers the same questions differently. One service returns 400 with a list of strings, another returns 422 with a map, a third validates in the service and throws IllegalArgumentException that turns into a 500. Clients and the API gateway end up with special cases for every service. A validation standard is a short document plus some shared code that settles these questions once. Interviewers ask it because it tests whether we can separate kinds of checks and give each a clear home. In this answer, we will separate input checks from business rules, then go through where validation happens, the error format, shared constraints, business rule errors, and how we keep tests and the API spec in line with the rules.
1Two Kinds of Checks
Before writing rules, we need to separate two kinds of checks. Input validation asks whether a request is well formed on its own: a required field is present, a quantity is positive, a string is not longer than 100 characters, a currency code is a real code. It needs nothing except the request itself, so it can run first and fast.
Business rules ask whether the operation is allowed in the current state of the world: does the account have enough money, is the order still open, has this customer reached a limit. They need data, often a transaction, and they change when the business changes. Mixing the two is what makes validation messy, so the standard gives each its own place.
| Check | Where | Mechanism | Status |
|---|---|---|---|
| required, size, range, format | request DTO at the boundary | Bean Validation, @Valid | 400 |
| currency, country, IBAN format | request DTO | shared constraint library | 400 |
| state rules: balance, order status | application service | typed BusinessException | 409 or 422 |
| entity not found | application service | NotFoundException | 404 |
| invariants that must always hold | domain objects | constructor checks | 500 if broken |
2Rule 1: Validate Input at the API Boundary
Every endpoint receives a request DTO, a plain record that describes exactly what the client may send, and that DTO carries Bean Validation annotations. The controller marks it with @Valid. We never bind requests straight onto JPA entities, because then the entity's shape becomes the API and validation rules leak into persistence.
The standard also covers request parameters and path variables. Since Spring Framework 6.1, constraint annotations placed directly on controller parameters, such as @Min(1) on a page parameter, are validated by Spring MVC itself, so the standard says not to put class-level @Validated on controllers. Message consumers and batch jobs are entry points too and follow the same rule with an injected validator.
3Rule 2: One Error Format With Field Paths and Codes
All services return errors as ProblemDetail, the RFC 9457 format that Spring supports natively, with the media type application/problem+json. The standard fixes the extra properties: a top-level code, a traceId, and for validation an errors list in which each entry has a field path, a code and a message.
Notice that the field path uses the JSON names the client sent, including nesting such as items[2].quantity, and that the code is a stable identifier while the message is only for humans. Clients write logic against code and field and show message to people. Rejected values are left out by default, because they may contain personal data.
4Rule 3: Shared Constraints in a Library
Rules that many services need, such as currency codes, country codes, IBANs or phone numbers, come from one constraints library owned by a platform team. Each constraint returns true for null so it combines with @NotNull, its reference data has a named source and a refresh process, and its message code is fixed. The same library ships the error advice that produces the format above, so a service that adds the dependency gets the standard behaviour without writing a handler.
This is the rule that keeps the standard alive. A written standard that every team must implement by hand drifts within a year; a dependency that does the right thing by default does not.
5Rule 4: Business Rules in Services With Typed Errors
Business rules are checked in the application service, inside the transaction, where the current data is available. When one fails, the service throws a typed exception that carries a stable error code, never a generic IllegalArgumentException. The shared advice maps the type to a status and to the same ProblemDetail shape.
The reason for typed errors is that the code is part of the API contract. A client can react to insufficient_funds by showing a top-up screen, which it could never do with a string message. The standard fixes the status mapping too, for example 404 for not found, 409 for a conflict with the current state, and 422 for a well-formed request the rules refuse.
6Rule 5: Test the Rules and Keep the OpenAPI Spec in Line
Every validated DTO has a web test that sends invalid input and asserts both the 400 status and the error body, so the format cannot silently change. Every business error has a service test that checks the code.
The OpenAPI spec is the promise clients read, so it must say what validation does. Tools such as springdoc read Bean Validation annotations and turn @Size(max = 64) into maxLength: 64 and @Positive into a minimum, which keeps the spec generated from the same source as the rules. The spec also declares the shared error schema for 400, 404, 409 and 422 responses, and a linter in the build fails any API that does not reference it.
7Follow-up questions
Should validation errors return 400 or 422?
The standard should pick one and use it everywhere. A common choice, and the one Spring uses by default, is 400 for malformed input such as a failed @Valid, and 422 for requests that are well formed but refused by a business rule. What matters more than the exact choice is that every service makes the same one.
Do we validate again in the service if the controller already validated?
For input format, the boundary check is enough for HTTP. But services that are also called by consumers or jobs use method validation with @Validated, and domain objects keep their invariants in their constructors, so a caller that skips the boundary still cannot create invalid state.
How do we stop rejected values leaking personal data?
The standard says the errors list holds field, code and message, never the rejected value, and the shared advice enforces that. Logs follow the same rule: we log field names and codes for validation failures, not the values, unless the field is known to be safe.
8Conclusion
In this answer, we defined a validation standard. Input is validated at the boundary with Bean Validation on request DTOs, errors use one ProblemDetail format with field paths, stable codes and messages, common rules come from a shared constraints library that also ships the error advice, business rules live in services and throw typed errors with codes, and tests plus a generated, linted OpenAPI spec keep the rules and the documentation in step.
Open with
I'd start by separating input validation from business rules. Input validation, meaning required fields, sizes, ranges and formats, happens at the API boundary with Bean Validation on request DTOs and @Valid, and it fails fast with a 400.
Then, if they want more
We never bind straight onto entities, and controllers don't use class-level @Validated, because since Spring 6.1 MVC validates parameter constraints itself.
Every service returns one error format: ProblemDetail, with a stable top-level code, a trace id, and for validation an errors list where each entry has the field path as the client sent it, a code and a message, but not the rejected value.
Common rules like currency, country and IBAN come from a shared constraints library that also ships the error advice, so the standard is the default rather than something each team rebuilds. Business rules that need current data live in the application service inside the transaction, and they throw typed exceptions that carry a code, mapped to 404, 409 or 422.
Finally, each DTO has a web test asserting the status and body, and the OpenAPI spec is generated from the same annotations and declares the shared error schema, with a linter in the build.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Framework 6
- 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 "Define a validation standard for all services: where validation happens, error format...": https://scrivio-interview-library.netlify.app/spring-boot/request-validation/9611c11ce3/