Quick revision
The short answer
When @Valid @RequestBody fails, Spring throws MethodArgumentNotValidException. We handle it in an @ExceptionHandler, or by overriding handleMethodArgumentNotValid in an advice that extends ResponseEntityExceptionHandler, read ex.getBindingResult().getFieldErrors() for each field name and message, add getGlobalErrors() for object-level errors, and return a 400 ProblemDetail with an errors property listing them.
What to remember
In this answer, we turned a bare 400 into a useful one. A failed @Valid @RequestBody throws MethodArgumentNotValidException, whose BindingResult holds field errors and global errors. We handled it with an @ExceptionHandler, or by overriding handleMethodArgumentNotValid in a ResponseEntityExceptionHandler subclass, mapped getFieldErrors() and getGlobalErrors() to field and message pairs without rejected values, and returned a 400 ProblemDetail with an errors property.
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.
When @Valid @RequestBody fails, Spring throws MethodArgumentNotValidException. We handle it in an @ExceptionHandler, or by overriding handleMethodArgumentNotValid in an advice that extends ResponseEntityExceptionHandler, read ex.getBindingResult().getFieldErrors() for each field name and message, add getGlobalErrors() for object-level errors, and return a 400 ProblemDetail with an errors property listing them.
Why this question is asked
The default 400 for an invalid body says very little: Invalid request content, or Spring Boot's generic Bad Request JSON. A client that sent five fields has no idea which one was wrong, so a mobile app cannot highlight the input and a partner has to guess. Returning the list of invalid fields is one of the most useful small improvements we can make to an API. Interviewers ask it as a coding question to see whether we know which exception to handle and where the errors live inside it. In this answer, we will look at the default response, see where the field errors are stored, write the handler in both styles, and include object-level errors.
1The Default Response for an Invalid Body
Here is a request DTO with constraints and a controller that validates it.
The status is right, but the client cannot tell that all three fields failed. The information exists on the server; we just need to put it into the response.
2Where the Field Errors Live
When Spring MVC reads the body, it validates it because of @Valid and collects the problems in a BindingResult, Spring's container for validation errors of one object. Since the method has no BindingResult parameter to receive it, Spring throws MethodArgumentNotValidException, which carries that BindingResult.
Inside it there are two kinds of errors. A FieldError belongs to one field and has the field name, the rejected value, an error code such as NotBlank and a message. An ObjectError without a field, called a global error, belongs to the object as a whole, for example from a class-level constraint that checks two dates are in order.
- MethodArgumentNotValidExceptionthrown for
@Valid@RequestBody - BindingResult
getBindingResult(): all errors for the body object - FieldError (getFieldErrors)field, code,
defaultMessage,rejectedValue - ObjectError (getGlobalErrors)object name, code,
defaultMessage
4 layers, from the top down. Step through them one at a time.
3Writing the Handler With @ExceptionHandler
In a @RestControllerAdvice, we handle the exception, turn each error into a small record, and return a ProblemDetail with an errors property.
getField() returns the property path, so a nested error appears as items[2].quantity. getDefaultMessage() is the message Bean Validation already interpolated, such as must be greater than 0. Notice that we do not include getRejectedValue(): the client already knows what it sent, and echoing values can copy passwords or personal data into logs.
4What the Client Receives
Now the same request produces a response the client can act on, one entry per problem.
The order of field errors is not guaranteed, so clients should not rely on it; if tests compare the list, sort it by field first.
5The Same Result by Overriding handleMethodArgumentNotValid
If our advice extends ResponseEntityExceptionHandler, that base class already handles MethodArgumentNotValidException, so we must not add a second @ExceptionHandler for it; Spring would reject the ambiguous mapping at startup. Instead we override the protected method and enrich the ProblemDetail the exception already carries.
Both styles give the same body. The second keeps all of Spring's standard exception handling in one place and passes through handleExceptionInternal, where we may add shared fields such as a traceId. If our controllers also put constraints directly on parameters, Spring Framework 6.1 and later can report the failure as HandlerMethodValidationException instead, which we handle the same way.
6Follow-up questions
How do we return a code per field as well as a message?
FieldError.getCode() returns the constraint name, such as NotBlank or Positive, which we can map to our own stable codes like required or must_be_positive. Clients then match on the code and only display the message.
How do we translate the messages?
Instead of getDefaultMessage(), we resolve each error through the MessageSource with the request locale, messageSource.getMessage(fe, locale). FieldError implements MessageSourceResolvable, so Spring tries its codes, such as Positive.createOrder.quantity and then Positive, before falling back to the default message.
What about invalid JSON, such as a string where a number is expected?
That fails before validation, while reading the body, and raises HttpMessageNotReadableException rather than MethodArgumentNotValidException. It is also a 400, and we can handle it separately, for example by naming the field from the Jackson cause when it is available, but without echoing parser details.
7Conclusion
In this answer, we turned a bare 400 into a useful one. A failed @Valid @RequestBody throws MethodArgumentNotValidException, whose BindingResult holds field errors and global errors. We handled it with an @ExceptionHandler, or by overriding handleMethodArgumentNotValid in a ResponseEntityExceptionHandler subclass, mapped getFieldErrors() and getGlobalErrors() to field and message pairs without rejected values, and returned a 400 ProblemDetail with an errors property.
Open with
When a @Valid @RequestBody fails, Spring throws MethodArgumentNotValidException, which carries the BindingResult with all the problems. I'd handle it in a @RestControllerAdvice with an @ExceptionHandler for that exception.
Then, if they want more
From ex.getBindingResult().getFieldErrors() I map each FieldError to a small record with the field, which is the property path like items[2].quantity, and the default message, which Bean Validation has already interpolated, like must be greater than 0. I also add getGlobalErrors(), the object-level errors from class-level constraints, using the object name as the field.
Then I create a ProblemDetail with forStatusAndDetail of BAD_REQUEST, a title, and setProperty errors with the list, and return it, so the client gets a 400 application/problem+json body with one entry per invalid field. I leave out the rejected values, because the client already has them and they can contain personal data.
If my advice extends ResponseEntityExceptionHandler, I don't add a second handler, since that's an ambiguous mapping; I override handleMethodArgumentNotValid, enrich ex.getBody() the same way and return through handleExceptionInternal. For stable client logic I'd also include a code per field from getCode, and translate messages through the MessageSource. Parameter constraints can arrive as HandlerMethodValidationException, so I handle that too.
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 "Return a 400 response listing each invalid field and its message when @Valid...": https://scrivio-interview-library.netlify.app/spring-boot/error-handling/205b1428b9/