In Spring MVC you validate form input by annotating a model class with Jakarta Bean Validation constraints, adding @Valid to the @ModelAttribute parameter, and placing a BindingResult parameter directly after it. If bindingResult.hasErrors() is true you re-render the form, and the view shows the messages next to each field.
This applies to server-rendered Spring MVC with Thymeleaf or JSP. For JSON APIs the mechanism is the same, but errors surface as MethodArgumentNotValidException instead of BindingResult. Since Spring 6 and Spring Boot 3 the annotations come from jakarta.validation, not javax.validation.
With Spring Boot, add the web, Thymeleaf and validation starters. The validation starter pulls in Hibernate Validator, the reference implementation.
Without Boot, add Hibernate Validator and an EL implementation yourself and declare a LocalValidatorFactoryBean bean. Spring MVC picks it up automatically.
Put constraints on the class that backs the form. Messages can be literal strings or keys resolved from messages.properties.
Use @NotBlank for strings. @NotNull accepts an empty string, which is almost never what a form needs. Use wrapper types such as Integer for numbers so a missing value is null rather than 0.
Two rules matter here. The BindingResult must come immediately after the validated parameter, otherwise Spring throws MethodArgumentNotValidException or BindException instead of letting you handle it. And on success, redirect rather than render, so a browser refresh does not resubmit the form.
Thymeleaf reads errors through the #fields helper and the th:errors attribute. The original JSP-style example in many tutorials mixes both syntaxes, so here is the correct Thymeleaf form:
th:field re-fills the submitted value, so users do not lose their input. For JSP, use the Spring form tag library instead, with <form:form modelAttribute="userForm"> and <form:errors path="name"/>.
A custom annotation plus a ConstraintValidator handles rules the built-ins cannot express.
Return true for null and add @NotNull separately when the field is mandatory. That keeps each constraint focused on one rule. Spring creates validators through the application context, so you can inject beans into them.
Some rules involve several fields, such as "password and confirmation match". Either write a class-level constraint, or implement Spring's Validator interface and register it for a controller with @InitBinder.
You can also add errors by hand inside the handler, for example after a database uniqueness check:
rejectValue attaches the error to a field, so th:errors displays it like any annotation-based error.
For @RequestBody JSON endpoints, BindingResult is optional. Without it Spring throws MethodArgumentNotValidException, which you handle once for every controller.
Do not use this for HTML forms. A form should redisplay with the user's input and inline messages, which only BindingResult provides.
It must be declared right after the @Valid parameter. If another parameter such as Model sits between them, Spring cannot associate the two and throws an exception.
Browsers display whatever page you return, and returning the form view with status 200 is the common approach. You can set the response status to 400 with @ResponseStatus or by using ResponseEntity if you prefer.
Yes. Write message = "{user.name.required}" in the annotation and define the key in messages.properties, or place it in ValidationMessages.properties. This also enables localisation.
@Valid is the standard annotation and cascades into nested objects. @Validated is Spring's own and also supports validation groups.
Annotate the form model with jakarta.validation constraints, add @Valid plus a directly following BindingResult in the handler, and re-render the view when errors exist. Display messages with th:errors, add custom constraints or an @InitBinder validator for complex rules, and redirect after a successful post. Use @RestControllerAdvice for JSON endpoints.