The @Valid annotation tells Spring to run Bean Validation on an object before your code uses it. Put it on a @RequestBody, form-bound object or nested field, and Spring checks the constraints declared on that type, such as @NotBlank, @Size and @Email. If any constraint fails, the request is rejected with a validation exception, which Spring MVC turns into a 400 Bad Request by default.
@Valid comes from the Jakarta Bean Validation standard (jakarta.validation.Valid). In Spring Boot 2 and earlier it was javax.validation.Valid; the behaviour is the same.
@Valid marks a parameter, field or return value for cascading validation. On a controller parameter, Spring MVC validates the bound object right after data binding and before your method body runs. The constraints themselves live on the class being validated, and the validator implementation (Hibernate Validator in Boot) enforces them.
Common constraint annotations:
| Annotation | Checks |
|---|---|
@NotNull | Value is not null |
@NotBlank | String is not null and contains a non-whitespace character |
@NotEmpty | String, collection or array is not empty |
@Size(min, max) | Length or size is within the range |
@Min / @Max | Number is within bounds |
@Email | Looks like an email address |
@Pattern(regexp) | String matches a regular expression |
@Past / @Future | Date is in the past or future |
Since Spring Boot 2.3, validation is no longer included in spring-boot-starter-web. Add the validation starter:
Without it, annotations such as @NotBlank compile but are not enforced.
Define the constraints on a DTO. A record keeps it short:
Then add @Valid on the controller parameter:
Sending a username of two characters makes Spring throw MethodArgumentNotValidException before register is called, and the client receives a 400 response.
The default error body for validation failures is minimal. Handle the exception to return field-level messages:
Spring 6 can also produce RFC 9457 problem details; enable them with spring.mvc.problemdetails.enabled=true. For server-rendered forms, place a BindingResult parameter directly after the @Valid parameter. The method then runs even when validation fails, and you inspect result.hasErrors() yourself.
Validation does not cascade into nested objects automatically. Annotate the nested field with @Valid:
@Validated is Spring's own annotation (org.springframework.validation.annotation.Validated). The differences:
@Valid | @Validated | |
|---|---|---|
| Source | Jakarta Bean Validation | Spring |
| Validation groups | No | Yes |
| Use on a class for method validation | Not for this | Yes |
| Cascades into nested objects | Yes, on fields | No, use @Valid on the field |
To validate simple parameters such as @RequestParam @Min(1) int page or @PathVariable, constraints on method parameters are supported. Spring 6.1 and later applies built-in method validation to controllers and reports failures as HandlerMethodValidationException, while older versions require @Validated on the class and throw ConstraintViolationException.
Usually the validation starter is missing, the constraints are on the wrong class, or a nested object lacks its own @Valid. Also check that you imported jakarta.validation.Valid on Spring Boot 3.
@NotNull rejects only null, @NotEmpty also rejects empty values, and @NotBlank also rejects whitespace-only strings. For text input, @NotBlank is usually right.
400 Bad Request for request bodies and bound models. You can customise the body with a @RestControllerAdvice.
Yes. Create an annotation marked with @Constraint(validatedBy = ...) and implement ConstraintValidator.
@Valid triggers Bean Validation on controller arguments and nested objects, so invalid input never reaches your logic. Add spring-boot-starter-validation, put constraints on DTOs, handle MethodArgumentNotValidException for clear error responses, and use @Valid on nested fields and @Validated when you need groups.