In Spring, @Valid tells the framework to run Jakarta Bean Validation on an object and, when applied to a field, to cascade validation into the nested object. On a controller parameter such as @Valid @RequestBody UserDto dto, Spring validates the object against its constraint annotations before your method runs. If any constraint fails, the method is not called and Spring responds with 400 Bad Request.
Since Spring Boot 3, the annotation is jakarta.validation.Valid. Earlier Boot 2.x versions used javax.validation.Valid.
@Valid is defined by the Jakarta Bean Validation specification, not by Spring. It has two jobs:
The constraints themselves, such as @NotBlank, @Email, @Size and @Min, live on the class being validated. @Valid only says "check this one".
Spring Boot no longer includes the validator in the web starter. Add the validation starter, which brings Hibernate Validator:
Without it, annotations are ignored or no validator is found.
Records work well as request DTOs, because the constraints go on the components.
Posting {"email":"not-an-email","password":"123","age":10} fails validation, and the controller body never runs. Prefer @NotBlank for strings: @NotNull accepts an empty string, while @NotBlank rejects null, empty and whitespace-only values.
@Valid on a field validates the nested object. Without it, constraints inside Address are ignored.
Note that @Valid does not imply @NotNull. If the nested object can be null, add @NotNull, otherwise a null address passes. List<@Valid OrderLine> validates every element of the list.
@Valid on a service method parameter does nothing by itself. Method-level validation needs @Validated on the class, which makes Spring create a validating proxy.
On a failure the proxy throws jakarta.validation.ConstraintViolationException.
@Valid | @Validated | |
|---|---|---|
| Origin | Jakarta Bean Validation | Spring |
| Validation groups | No | Yes, @Validated(OnCreate.class) |
| Cascades to nested objects | Yes, on fields | No |
| Enables method validation on a class | No | Yes, at class level |
Use @Valid for the standard behaviour and cascading, and @Validated when you need groups or class-level method validation.
For a failing @Valid @RequestBody, Spring throws MethodArgumentNotValidException. Spring 6.1 and later also validate constraints placed directly on controller parameters, such as @RequestParam @Min(1) int page, and raise HandlerMethodValidationException. Handle both in one place, returning a structured error.
A client then receives an RFC 9457 style application/problem+json body with a field-to-message map.
Hibernate also runs Bean Validation before persisting or updating an entity when a validator is on the classpath. A violation there throws ConstraintViolationException. Do not rely on this as your only input check: validate request DTOs at the web boundary so users get a clean 400 instead of a failed transaction.
Yes for body and model objects. Without @Valid (or @Validated) on the parameter, the constraint annotations on the class are not checked.
Put @Valid on the nested field, or on the container element type. Also check that the validation starter is on the classpath.
@NotNull rejects null only. @NotEmpty also rejects empty strings and collections. @NotBlank applies to strings and also rejects whitespace-only values.
jakarta.validation.Valid. The javax.validation package belongs to Spring Boot 2 and earlier.
@Valid triggers Jakarta Bean Validation on a parameter and cascades into nested objects. Add spring-boot-starter-validation, put constraints on DTO fields, use @NotBlank for strings, and handle MethodArgumentNotValidException in a @RestControllerAdvice. For service-layer validation or validation groups, add @Validated to the class.