When to Use @Validated vs @Valid for Nested Validation in Spring
This article explains the differences between Spring's @Validated and the standard @Valid annotations, covering grouping support, where each can be applied, and how to enable nested validation for complex objects in controller parameters.
Spring Validation provides two main annotations for parameter validation: @Validated (Spring's JSR‑303 variant) and @Valid (the standard JSR‑303 annotation), which can be combined with BindingResult to obtain validation results.
1. Grouping
@Validatedsupports validation groups, allowing different constraints to be applied based on the group used; @Valid does not support grouping.
2. Annotation Placement
@Validatedcan be placed on a class, method, or method parameter, but it cannot be applied to a field (member property). @Valid can be placed on methods, constructors, method parameters, and also on fields, enabling field‑level validation.
3. Nested Validation
Consider the following entity classes:
public class Item {
@NotNull(message = "id不能为空")
@Min(value = 1, message = "id必须为正整数")
private Long id;
@NotNull(message = "props不能为空")
@Size(min = 1, message = "props至少要有一个自定义属性")
private List<Prop> props;
}
public class Prop {
@NotNull(message = "pid不能为空")
@Min(value = 1, message = "pid必须为正整数")
private Long pid;
@NotNull(message = "vid不能为空")
@Min(value = 1, message = "vid必须为正整数")
private Long vid;
@NotBlank(message = "pidName不能为空")
private String pidName;
@NotBlank(message = "vidName不能为空")
private String vidName;
}When a controller receives an Item object, using only @Validated or @Valid on the method parameter validates the id and the props collection itself, but it does **not** automatically validate the fields of each Prop inside the list.
To enable nested validation, the collection field must be annotated with @Valid:
public class Item {
@NotNull(message = "id不能为空")
@Min(value = 1, message = "id必须为正整数")
private Long id;
@Valid // enable nested validation
@NotNull(message = "props不能为空")
@Size(min = 1, message = "props至少要有一个自定义属性")
private List<Prop> props;
}Controller example:
@RestController
public class ItemController {
@RequestMapping("/item/add")
public void addItem(@Validated Item item, BindingResult bindingResult) {
// processing logic
}
}With the @Valid annotation on the props field, Spring Validation will traverse each Prop object and report violations (e.g., null or negative pid / vid) in the BindingResult.
Summary of Differences
@Validated : cannot be used on fields; does not trigger nested validation by itself; can be combined with validation groups.
@Valid : can be used on fields; enables nested validation when placed on a collection or object field; does not support groups.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
