Quick revision
The short answer
With @Validated on the controller class, Spring validates the @RequestParam through an AOP proxy, and the proxy throws ConstraintViolationException. Spring MVC has no default mapping for that exception, so it ends up as a 500, while invalid bodies raise MethodArgumentNotValidException, which is mapped to 400. The fix is to remove @Validated and use the built-in method validation of Spring Framework 6.1 and later, which raises HandlerMethodValidationException and maps it to 400, or to add an @ExceptionHandler that maps ConstraintViolationException to 400.
What to remember
In this answer, we saw that one controller was using two validation mechanisms. The body was validated by Spring MVC and failed with MethodArgumentNotValidException, which maps to 400. The request parameter was validated by the AOP proxy that class-level @Validated creates, and its ConstraintViolationException has no default mapping, so it became 500. Removing @Validated lets the built-in method validation of Spring Framework 6.1 and later raise HandlerMethodValidationException with a 400; adding a scoped handler for ConstraintViolationException is the other fix.
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.
With @Validated on the controller class, Spring validates the @RequestParam through an AOP proxy, and the proxy throws ConstraintViolationException. Spring MVC has no default mapping for that exception, so it ends up as a 500, while invalid bodies raise MethodArgumentNotValidException, which is mapped to 400. The fix is to remove @Validated and use the built-in method validation of Spring Framework 6.1 and later, which raises HandlerMethodValidationException and maps it to 400, or to add an @ExceptionHandler that maps ConstraintViolationException to 400.
Why this question is asked
This bug is common in services upgraded from older Spring Boot versions, where class-level @Validated was the only way to validate a plain request parameter. The client sends page=0, the rule @Min(1) correctly rejects it, and yet the client gets a 500 Internal Server Error, which tells it the server broke rather than that its input was wrong. Monitoring then counts client mistakes as server failures. Interviewers ask it because the answer shows whether we understand that two different validation mechanisms exist in Spring MVC and that each throws a different exception. In this answer, we will reproduce the problem, follow both paths to see why the status differs, and then go through the two fixes.
1Reproducing the Problem
Here is a controller written in the older style. The body is validated with @Valid, and the request parameter has a constraint, which only worked in older versions because of @Validated on the class.
Both requests break a constraint, and both are client mistakes. The difference in status tells us that the two failures travel through different code.
2Why the Body Becomes a 400
@Valid on a @RequestBody is handled by Spring MVC itself during argument resolution, the step where Spring turns the HTTP request into the method's arguments. After reading the JSON into a CreateOrder, the resolver runs the validator, and if there are errors it throws MethodArgumentNotValidException. This exception is one Spring MVC knows well: DefaultHandlerExceptionResolver, and ResponseEntityExceptionHandler if we use it, map it to 400 Bad Request.
3Why the Parameter Becomes a 500
Class-level @Validated works differently. It tells Spring's MethodValidationPostProcessor to wrap the controller in a proxy, a wrapper object generated at runtime, whose interceptor validates arguments before the method runs. This is general AOP method validation, the same mechanism used for service classes, and it knows nothing about HTTP. When @Min(1) fails, it throws jakarta.validation.ConstraintViolationException.
Spring MVC's default resolvers do not know this exception. It is a general validation exception that could come from deep inside a service, where a 500 would be correct, so Spring does not guess. With no handler for it, the exception reaches the servlet container and Spring Boot's /error page, which reports 500.
| @Valid @RequestBody | @Min @RequestParam, class @Validated | |
|---|---|---|
| Who validates | argument resolver in Spring MVC | AOP proxy around the controller |
| When | while building arguments | after arguments are built, before the method |
| Exception | MethodArgumentNotValidException | ConstraintViolationException |
| Default status | 400 | 500 (no default mapping) |
4Fix 1: Remove @Validated and Use Built-In Method Validation
Spring Framework 6.1, which Spring Boot 3.2 and later use, added method validation to Spring MVC itself. When a controller method has constraint annotations directly on its parameters, such as @Min on a @RequestParam, and a validator is configured (Boot configures one when the validation starter is present), Spring MVC validates them during the request. Failures raise HandlerMethodValidationException, which implements Spring's ErrorResponse with status 400, so the default resolvers return 400 Bad Request.
There is one condition: the built-in support stands aside when the controller has class-level @Validated, because the proxy would validate first. So the fix is to delete one annotation.
Notice one side effect. When method validation applies to a method because it has parameter constraints, Spring validates a @Valid body as part of that same step, so the error for the body may arrive as HandlerMethodValidationException rather than MethodArgumentNotValidException. If our error advice builds field errors from MethodArgumentNotValidException, it should handle both; ResponseEntityExceptionHandler has a handleHandlerMethodValidationException method we can override. Check your version for the exact behaviour.
5Fix 2: Map ConstraintViolationException to 400
If we cannot remove @Validated yet, for example because the controller relies on validation groups set at class level, we add a handler. We should be careful about scope. An advice limited with basePackages applies to exceptions from those controllers, whatever their origin, so a ConstraintViolationException thrown by a service deep inside the call, which may be a server bug, would also become a 400. That is a real cost of this fix and another reason to prefer Fix 1.
The property path here is list.page, the method name and parameter name, rather than the HTTP parameter name. That is one more reason the built-in validation is the better long-term fix: its errors know they came from a request parameter.
6Follow-up questions
Why did @Validated work fine before the upgrade?
It worked the same way; it always produced ConstraintViolationException. Teams usually had a handler for it somewhere, or accepted the 500. Before Spring Framework 6.1, class-level @Validated was the only way to validate a plain @RequestParam, so the handler was part of the standard setup. When a team removes that handler while cleaning up, or copies the controller into a new service without it, the 500 appears.
Do we still need @Valid on the request body after removing @Validated?
Yes. @Valid on a @RequestBody or @ModelAttribute is what asks Spring to validate the object's fields. Built-in method validation covers constraints declared directly on parameters, and it cascades into a parameter only when the parameter is marked @Valid.
How would you catch this kind of bug before production?
With a web test, for example @WebMvcTest or MockMvc, that sends an invalid parameter and asserts the status is 400 and the body has our error format. One such test per kind of input, body, parameter, path variable and header, catches any route that falls through to 500.
7Conclusion
In this answer, we saw that one controller was using two validation mechanisms. The body was validated by Spring MVC and failed with MethodArgumentNotValidException, which maps to 400. The request parameter was validated by the AOP proxy that class-level @Validated creates, and its ConstraintViolationException has no default mapping, so it became 500. Removing @Validated lets the built-in method validation of Spring Framework 6.1 and later raise HandlerMethodValidationException with a 400; adding a scoped handler for ConstraintViolationException is the other fix.
Open with
The two failures go through different mechanisms. The request body is validated by Spring MVC's argument resolver, which throws MethodArgumentNotValidException, and Spring maps that to 400. The @Min on the request parameter is enforced by the AOP proxy that class-level @Validated creates through MethodValidationPostProcessor.
Then, if they want more
That proxy is generic method validation, so it throws jakarta's ConstraintViolationException, and Spring MVC has no default mapping for it. It falls through to the error page as a 500. Since Spring Framework 6.1, which Boot 3.2 and later use, Spring MVC validates constraint annotations on controller parameters itself and throws HandlerMethodValidationException, which is a 400.
But it steps aside when the controller has class-level @Validated. So my preferred fix is to remove @Validated from the controller. Then both cases are 400, and I update my error advice so it also formats HandlerMethodValidationException, because a body can be reported through it too when method validation applies.
If I can't remove @Validated yet, I add an @ExceptionHandler for ConstraintViolationException that returns 400, scoped to the web controllers, knowing that it would also turn a violation from deep in a service into a 400. And I'd add a MockMvc test per input type so this can't regress.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Framework 6, Spring Boot 3.2
- 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 "A controller annotated with @Validated returns 500 when @Min on a @RequestParam fails...": https://scrivio-interview-library.netlify.app/spring-boot/request-validation/6a46c7bdf9/