BindingResult is the Spring MVC object that holds the outcome of binding request data to a model attribute and validating it. It collects type-conversion failures (such as text in an int field) and Bean Validation violations (such as @NotBlank) so your controller method can inspect them and redisplay the form instead of failing with an exception.
You declare it as a method parameter placed directly after the @ModelAttribute or @Valid argument it describes. If you leave it out, Spring raises an exception (a BindException subclass) when binding or validation fails.
org.springframework.validation.BindingResult extends Errors and adds binding-specific behaviour. For every model attribute that Spring binds, a BindingResult is registered in the model under the key org.springframework.validation.BindingResult.<attributeName>, which is how view technologies such as Thymeleaf and JSP find the errors.
It records two kinds of problems:
abc submitted for an Integer field (a typeMismatch error).@Size or @Email constraint that failed, or an error added by a custom Validator.Each problem is stored as a FieldError (tied to a property) or an ObjectError (a global error about the whole object).
The rule that trips up most developers is positional: the BindingResult must come immediately after the model attribute it belongs to. With two attributes you need two BindingResult parameters, each directly after its object.
The jakarta namespace applies to Spring Boot 3 and later; Spring Boot 2 uses javax.validation. Records work as form objects in recent Spring versions because data binding uses the canonical constructor.
When the form is valid, redirect after the POST (the Post/Redirect/Get pattern) so a browser refresh does not resubmit it.
Thymeleaf reads the BindingResult from the model for you. Use #fields.hasErrors and th:errors next to each input.
th:field re-populates the submitted value, so users do not lose their input when validation fails.
| Method | What it returns |
|---|---|
hasErrors() | true if any binding or validation error exists |
hasFieldErrors("email") | true if that specific property has an error |
getAllErrors() | Every ObjectError, including field errors |
getFieldErrors() | Only the FieldError entries |
getFieldError("email") | The first error for one property |
getGlobalErrors() | Errors not tied to a property |
rejectValue(field, code, message) | Adds a field error manually |
reject(code, message) | Adds a global error manually |
Rules that need a database or cross-field logic are often easier to express in the controller or a Validator, and you report them with rejectValue:
Checking hasFieldErrors first avoids a pointless database lookup when the value already failed basic validation.
BindingResult is meant for server-rendered forms where you want to redisplay the page. For JSON APIs the picture is different.
@RequestBody @Valid, a failure throws MethodArgumentNotValidException. You can add a BindingResult parameter to handle it inline, but most APIs let the exception propagate to a @RestControllerAdvice that returns a structured ProblemDetail response.@Validated on a class and constraints on method parameters, violations surface as a ConstraintViolationException or, in Spring 6.1 and later, HandlerMethodValidationException, and BindingResult is not involved.BindingResult after a @RequestBody argument, Spring will not throw, so you must check hasErrors() yourself or invalid data will flow into your service.Model, between the object and its BindingResult. The error is a failed binding exception rather than a clear message.BindingResult but forgetting @Valid. Binding errors are captured, but constraint annotations are never checked.hasErrors() and processing the object anyway.BindingResult for a @RequestParam. It only pairs with model attributes and request bodies.Spring resolves the argument by position: it looks at the parameter immediately before it to know which attribute the errors belong to. A parameter in between breaks that link.
Not strictly. Without it, a failure throws an exception that you can handle globally. Add it when you want to redisplay a form or customise the response in the same method.
Errors is the basic interface for storing and reading errors. BindingResult extends it with binding details such as the target object and the ability to get the raw rejected value. In controllers you can declare either type, and they behave the same.
Yes, with @RequestBody @Valid, but a global exception handler returning ProblemDetail is the more common and cleaner design.
BindingResult captures binding and validation errors for one model attribute so your controller can decide what to do. Put it directly after the validated parameter, check hasErrors() before using the data, expose errors to the view with th:errors or the equivalent, and add custom errors with rejectValue. For JSON APIs prefer a central exception handler.