DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk4 min

How to Handle Spring MVC @Valid Errors with BindingResult

Put BindingResult immediately after a validated Spring MVC argument to handle its errors locally. Direct method constraints can use a different validation path and exception.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

@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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To 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.

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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
@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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.