In Spring MVC you validate input by adding Jakarta Bean Validation annotations (@NotBlank, @Size, @Email) to a model class, marking the controller parameter with @Valid, and checking a BindingResult placed right after it. If bindingResult.hasErrors() is true, you return the form view again and show the messages. For cross-field or database-backed rules, write a custom ConstraintValidator or a Spring Validator.
This works for both server-rendered forms (Thymeleaf, JSP) and REST controllers. Spring 6 and Spring Boot 3 use the jakarta.validation package; Spring 5 and Spring Boot 2 use javax.validation with identical annotation names.
In Spring Boot, add the starter and the validator is configured automatically:
In a plain Spring MVC application, add jakarta.validation:jakarta.validation-api and Hibernate Validator, then register a LocalValidatorFactoryBean. With @EnableWebMvc or Spring Boot, Spring MVC picks it up automatically when Hibernate Validator is on the classpath.
Use @NotBlank for text. @NotNull accepts an empty string, and @NotEmpty accepts whitespace only.
The BindingResult parameter must come immediately after the @Valid model attribute. If you place anything between them, Spring throws a MethodArgumentNotValidException/BindException instead of letting you handle the errors.
In a Thymeleaf template, show messages next to each field:
After a successful POST, redirect (Post/Redirect/Get) so that refreshing the page does not resubmit the form.
For @RestController methods, use @Valid @RequestBody. Without a BindingResult, Spring raises MethodArgumentNotValidException and returns 400 by default, which you can customize centrally:
Rules that compare fields or look up data do not fit single-field annotations. Implement Spring's Validator interface and run it from an @InitBinder method:
addValidators keeps the default Bean Validation in place and adds yours on top. Using setValidator would replace it. Alternatively, write a class-level custom annotation with a ConstraintValidator, which is reusable across controllers.
Use groups when the same class has different rules for create and update. Define marker interfaces and select them with @Validated:
For localized messages, use keys such as message = "{username.required}" and define them in ValidationMessages.properties, or configure Spring's MessageSource and expose it with a LocalValidatorFactoryBean.
Spring associates a BindingResult with the model attribute that directly precedes it. If another parameter sits in between, Spring cannot find it and throws an exception instead of populating the errors.
@Valid is the standard Jakarta annotation. @Validated is Spring's version and additionally supports validation groups and class-level method validation.
Client-side checks improve usability, but they can be bypassed, so server-side validation is mandatory.
Add a constraint such as @Min(1) on the parameter and put @Validated on the controller class. Spring 6.1 and later also validates controller method parameters natively.
Annotate the model with Jakarta constraints, mark the controller parameter with @Valid, and read errors from the BindingResult that immediately follows it. Redisplay the form on errors and redirect after success. Use a Spring Validator or a custom ConstraintValidator for cross-field rules, and centralize REST error output in a @RestControllerAdvice.