Comparison of Valid and Validated in Java
- 2021-08-12 02:40:06
- OfStack
If you want to add interface validation, you need
1. Add the @ Valid annotation before the request parameter in the interface method, without adding the @ Valid annotation on the implementation class of the interface, and without the @ Validated annotation:
addAnimal(@Valid Animal a)
2. Add the @ Valid annotation on each field to be validated in the request object class, and you don't need to add the @ Validated annotation on the class:
public class Animal{
@Valid
@NotEmpty
private String name;
}
Validated is an extension of Spring to Valid of javax. validation, adding the function of supporting packet verification, but not nested verification.
Valid supports nested validation, such as:
@Valid
private List<Animal> list
Extension of knowledge points:
@ Valid is used when using hibernate validation
@ Validated is used only with the spring Validator verification mechanism
<span style="font-size:18px;"> <dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.1.Final</version>
</dependency></span>