Spring boot REST validation error response
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Validation in a Spring Boot REST API is only half the job. The other half is turning validation failures into a response shape that clients can rely on instead of exposing a noisy default exception payload.
Triggering Validation on Request Bodies
For JSON request payloads, the usual pattern is Bean Validation plus @Valid on the controller method parameter.
Then in the controller:
If the JSON is invalid, Spring MVC raises MethodArgumentNotValidException for that request body.
Why the Default Error Response Is Often Not Enough
Spring Boot can already return an error response when validation fails, but the default structure is not always ideal for public APIs. Clients usually need something stable and small, such as:
- a top-level error code
- a human-readable message
- field-specific validation errors
That is why most APIs add a global exception handler.
Build a Consistent Error Payload
Start with simple response records:
Then map the exception in a @RestControllerAdvice:
Now the client gets a stable contract rather than a framework-specific blob.
Example Response
For a request such as:
The API can return:
That format is easier for front-end code and other API consumers to process consistently.
Validation Beyond Request Bodies
Spring also supports method and parameter validation. For example, path variables and query parameters can use constraints when the controller is annotated with @Validated.
Recent Spring versions may raise HandlerMethodValidationException for those method-parameter cases, not just MethodArgumentNotValidException. If your API uses both body validation and parameter validation, handle both deliberately.
Practical Design Advice
A good validation error response should be:
- stable across endpoints
- small enough for clients to parse easily
- specific enough to tell the caller which fields failed
- decoupled from internal exception class names
Do not leak framework internals into the public API contract unless that is a conscious choice.
Common Pitfalls
The most common mistake is using @Valid and stopping there, then discovering later that the default response shape is inconsistent with the rest of the API.
Another mistake is returning only a generic "Bad Request" message. Clients need field-level detail if they are expected to fix the request programmatically or show accurate form messages.
Developers also often handle only MethodArgumentNotValidException and forget method-level validation failures on query parameters or path variables.
Finally, keep the response schema stable. Changing the validation error format between endpoints makes client code harder than it needs to be.
Summary
- Use Bean Validation annotations and
@Validto trigger request-body validation in Spring Boot. - Catch
MethodArgumentNotValidExceptionin a@RestControllerAdviceto shape a consistent API response. - Include field names and messages so clients can react intelligently.
- If you validate query or path parameters, also consider
HandlerMethodValidationException. - Treat validation errors as part of the API contract, not just as thrown exceptions.
Related reading
- Spring Boot Swagger UI. Set JWT token
- Spring boot Webclient's retrieve vs exchange
- Spring Boot with embedded Tomcat behind Apache proxy
- Spring Cloud or Spring Boot? what is right spring project for developing Biz API's?
- Spring Boot Security CORS
- Spring boot Security Disable security
- Spring boot startup error for AWS application There is not EC2 meta data available
- Spring boot taking long time to start

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.