The @Valid annotation tells Spring to run Jakarta Bean Validation on an object before your code uses it. When you put it on a controller parameter such as a @RequestBody, Spring checks every constraint annotation on that object (@NotBlank, @Size, @Email and so on) and rejects the request when any of them fails. It also cascades validation into nested objects and collections.
@Valid comes from the Jakarta Bean Validation specification (jakarta.validation.Valid), not from Spring itself. Spring Boot 3 and later use the jakarta.* package; Boot 2 and older code use javax.validation.Valid.
@Valid triggers validation in a controller@Valid versus @ValidatedSpring Boot does not enable validation by default. Add the validation starter, which brings in Hibernate Validator, the reference implementation of the specification.
Without this dependency, constraint annotations compile but are silently ignored at runtime, which is a common reason why "validation does nothing".
Constraints go on the fields (or record components) of the object you want to validate. A Java record keeps the example short and immutable.
Use @NotBlank for strings: @NotNull accepts an empty string and @NotEmpty accepts a string of spaces.
@Valid triggers validation in a controllerAnnotate the parameter with @Valid. Spring MVC validates the object right after binding the JSON body and before your method body runs.
If validation fails for a @RequestBody, Spring throws MethodArgumentNotValidException and answers with HTTP 400 by default. For form binding with @ModelAttribute, you can add a BindingResult parameter right after the validated object and inspect the errors yourself instead of letting an exception be thrown.
The default 400 body is generic. A @RestControllerAdvice turns the field errors into a response your clients can use.
Bean Validation does not look inside nested objects unless you ask it to. Put @Valid on the field that holds the nested object or collection.
Here @Valid on shippingAddress validates the address constraints, and @Valid on the list validates every OrderLine. Without those annotations, constraints inside Address and OrderLine are never checked.
@Valid versus @ValidatedBoth trigger validation, but they differ in a few ways.
| Aspect | @Valid | @Validated |
|---|---|---|
| Origin | Jakarta Bean Validation | Spring (org.springframework.validation.annotation) |
| Validation groups | Not supported | Supported |
| Nested cascading | Yes, on fields | No, not for fields |
| Class level for method validation | No | Yes, on the class |
Use @Valid for the common case and for cascading. Use @Validated when you need groups, or on a class so that Spring validates method parameters of beans, such as @PathVariable and @RequestParam constraints.
@Valid annotation ignored?Most often the spring-boot-starter-validation dependency is missing, or the import is the wrong package (javax versus jakarta). Also check that the object is a Spring-managed parameter and that the constraints are on the class being validated.
@Valid work on @RequestParam and @PathVariable?Constraints on those parameters need the controller class to be annotated with @Validated. Since Spring Framework 6.1, built-in method validation also covers them when constraint annotations are present on parameters, throwing HandlerMethodValidationException.
@NotNull, @NotEmpty and @NotBlank?@NotNull rejects only null. @NotEmpty also rejects empty strings and collections. @NotBlank applies to strings and also rejects values that contain only whitespace.
Yes. Define an annotation marked @Constraint(validatedBy = ...) and implement ConstraintValidator. It then works with @Valid like any built-in constraint.
@Valid asks Spring to run Jakarta Bean Validation on a parameter and, when placed on fields, to cascade into nested objects. Add the validation starter, declare constraints on your request types, annotate the controller parameter, and handle MethodArgumentNotValidException for readable errors. Reach for @Validated only when you need validation groups or class-level method validation.