Recommended Free Tools
To handle Bean Validation errors inside a Spring MVC controller, put an Errors or BindingResult parameter immediately after the validated argument. Without one, individual argument validation normally raises MethodArgumentNotValidException. If constraints are placed directly on controller method parameters or its return value, method validation may instead raise HandlerMethodValidationException.
How do I use @Valid with BindingResult in Spring MVC?
Annotate an eligible command-object argument—such as a @ModelAttribute, @RequestBody, or @RequestPart argument—with Jakarta @Valid or Spring @Validated. Put BindingResult or Errors directly after that argument so the controller can inspect its binding and validation errors.
As an Amazon Associate I earn from qualifying purchases.
@PostMapping("/users")
public String createUser(
@Valid @ModelAttribute("user") UserInput input,
BindingResult errors) {
if (errors.hasErrors()) {
return "users/form";
}
// Continue with the validated input.
return "redirect:/users";
}
BindingResult exposes errors through Spring’s Errors abstraction. Check hasErrors() before proceeding; individual errors can be examined through the result’s field and global error accessors. This keeps the decision about what response to return in the controller.
Free tools Windows power users keep installed
One-click scans. No signup required.
@Valid asks Spring to validate the object’s constraints, including constraints nested within it. It is not itself a constraint annotation. As Spring’s validation reference puts it: “@Valid is not a constraint annotation, but rather for nested constraints within an Object.” See the Spring MVC validation reference.
#1 Best Overall
Does BindingResult have to come immediately after the @Valid parameter?
Yes, for local handling of that argument’s individual validation errors, the Errors or BindingResult parameter must immediately follow the validated argument. Putting another parameter between them does not associate the result with that argument, so Spring may raise an exception instead of invoking the controller to handle those errors.
This adjacency rule also matters when method validation applies: Spring calls the controller only when all validation errors are on method parameters that have an immediately following Errors parameter. Errors on other parameters lead to HandlerMethodValidationException.
Why am I getting MethodArgumentNotValidException?
Spring raises MethodArgumentNotValidException when individual validation of an eligible argument fails and the errors are not handled through an adjacent Errors or BindingResult. For a @RequestBody argument, Spring documents that this exception produces HTTP 400 by default. See Spring’s @RequestBody reference.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Fix the driver behind crashes, sound loss and screen glitches3Clear out junk files and repair common Windows errorsTo handle the errors locally, add the adjacent result parameter. To handle failures centrally, use exception handling and account for the validation path your controller signature uses; do not assume every validation failure has the same exception type.
Rank #3
What is the difference between MethodArgumentNotValidException and HandlerMethodValidationException?
The exception depends on what Spring validates: an individual object argument, or the controller method’s parameters and return value. Direct constraints on method parameters can activate method validation, which also covers nested constraints reached through @Valid.
| Validation path | Typical trigger | Failure representation | Local handling |
|---|---|---|---|
| Individual argument validation | Constraints on an eligible command object, such as one passed with @Valid |
MethodArgumentNotValidException when errors are not handled locally |
An immediately following Errors or BindingResult can receive the errors |
| Method validation | Constraints directly on method parameters, such as @Min, or on a return value; nested constraints reached through @Valid are included |
HandlerMethodValidationException |
An adjacent Errors can handle errors for its parameter, but errors on another parameter cause the exception |
@Valid alone does not trigger method validation because it is not a constraint. Adding a direct constraint such as @NotNull can change which validation path applies. Spring recommends handling both MethodArgumentNotValidException and HandlerMethodValidationException, since controller signatures determine which can occur; the framework describes the exceptions as similar and suitable for nearly identical handling. The MVC method-validation path was added in Spring Framework 6.1. The Spring MVC validation reference also notes that class-level @Validated invokes AOP-based method validation; remove it from a controller when using built-in MVC method validation.
How do I return validation errors from a Spring @RequestBody?
For local handling, place BindingResult immediately after the @RequestBody argument. The controller can inspect the result and return an error response of its choosing. Without local handling, invalid request-body data normally results in MethodArgumentNotValidException and HTTP 400 by default.
Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Clear out junk files and repair common Windows errorsFree Scan →@PostMapping("/api/users")
public ResponseEntity<?> createUser(
@Valid @RequestBody UserInput input,
BindingResult errors) {
if (errors.hasErrors()) {
return ResponseEntity.badRequest().body(errors.getFieldErrors());
}
// Continue with the validated input.
return ResponseEntity.ok().build();
}
The response format is an application decision: exposing Spring’s error objects directly may not be the API contract you want. Convert errors to a deliberate response shape when clients need stable, controlled fields.
Where do binding errors come from?
Request binding converts incoming values—such as form fields, query parameters, or request data—into an object. Conversion or binding problems and Bean Validation constraint violations are represented through Spring’s error abstractions. LocalValidatorFactoryBean adapts Jakarta Bean Validation constraint violations into Spring FieldError instances and adds them to an Errors object.
A DataBinder can run configured validators: after binding, binder.validate() records validation failures, and binder.getBindingResult() provides the combined result. Validators can be registered through MVC configuration or locally with @InitBinder, and multiple validators can be combined. See Spring’s documentation on Java Bean Validation and validation, data binding, and type conversion.
How should I design objects that receive request data?
Treat bound request data as untrusted. Spring recommends immutable input objects, including records or primary-constructor classes, or dedicated input objects that contain only the fields the endpoint is meant to accept. This limits what request data can bind to. Annotated controllers receive a request-specific WebDataBinder; customize it with controller-level @InitBinder methods or controller advice when needed. See Spring’s data-binding guidance.
The Tool Desk
Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Which Spring documentation version should I use?
Match the reference documentation to the Spring Framework version used by your application. The cited MVC validation page is for 7.1.0-M1 and is marked development documentation; it identifies 7.0.9 as the latest stable version at the time of the documentation snapshot. Do not treat that milestone page as confirmation of behavior in a stable release without checking the corresponding stable reference.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




