@Valid:
The @Valid annotation is used for verification, and the package belongs to:.
① First, you need to add annotations to the corresponding fields of the entity class to act as verification conditions, such as: @Min, the following code (age belongs to attributes in the Girl class):
@Min(value = 18,message = "Minors are prohibited from entering") private Integer age;
② Secondly, add the @Valid annotation to the parameters to be checked by the controller layer method, and need to pass in the BindingResult object to obtain feedback information when the verification fails, as follows:
@PostMapping("/girls") public Girl addGirl(@Valid Girl girl, BindingResult bindingResult) { if(()){ (().getDefaultMessage()); return null; } return (girl); }
() is used to obtain the content in the message added on the corresponding field, such as: the content of the message attribute in the @Min annotation
@Validated:
@Valid is inside.
@Validated is a package of @Valid and is used by the verification mechanism provided by Spring. @Valid does not provide grouping function
@Validated Special Usage
1. Grouping
When an entity class requires multiple verification methods, for example: for the id of an entity class, it is not necessary when adding it, but is necessary when updating.
Verification can be grouped through groups
Group interface class (to assign different class objects to groups to achieve grouping purpose):
package ; public interface First { }
Entity Class:
package ; import ; import ; import ; public class People { // When first grouping, it is judged that it cannot be empty @NotEmpty(groups={}) private String id; //The name field is not empty and the length is between 3-8 @NotEmpty @Size(min=3,max=8) private String name; public String getName() { return name; } public void setName(String name) { = name; } public String getId() { return id; } public void setId(String id) { = id; } }
Note:
(1) Do not assign groups, and verification is required every time by default
(2) When multiple verification methods are required for a parameter, the purpose can also be achieved by assigning different groups. example:
@NotEmpty(groups={}) @Size(min=3,max=8,groups={}) private String name;
Control class:
package ; import ; import ; import ; import ; import ; import ; import ; @Controller public class FirstController { @RequestMapping("/addPeople") //No need to verify the ID public @ResponseBody String addPeople(@Validated People p,BindingResult result) { ("people's ID:" + ()); if(()) { return "0"; } return "1"; } @RequestMapping("/updatePeople") //Requires verification of ID public @ResponseBody String updatePeople(@Validated({}) People p,BindingResult result) { ("people's ID:" + ()); if(()) { return "0"; } return "1"; } }
Note:
@Validated When the groups attribute is not added, the default verification attribute is not grouped, such as this example: People's name attribute.
When @Validated does not add groups attribute, all parameters' verification types are grouped (that is, the @NotEmpty and @Size of People's name in this example are added groups attributes), so no parameters are verified.
2. Group sequence
By default, the order of constraint verification in different groups is unordered, but in some cases the order of constraint verification is important.
example:
(1) Constraint verification in the second group relies on a stable state to run, and this stable state is verified by the first group.
(2) Verification of a certain group is time-consuming, and the CPU and memory usage rate is relatively large. The best choice is to put it last for verification. Therefore, when performing group verification, an orderly verification method is still needed, which proposes the concept of group sequence.
A group can be defined as a sequence of other groups, and it must comply with the order specified in the sequence when used for verification. When using group sequence verification, if the group verification before the sequence fails, the subsequent groups will no longer be validated.
Group interface class (sort groups through @GroupSequence annotation):
package ; public interface First { }
package ; public interface Second { }
package ; import ; @GroupSequence({,}) public interface Group { }
Entity Class:
package ; import ; import ; import ; import ; public class People { // When first grouping, it is judged that it cannot be empty @NotEmpty(groups={}) private String id; //The name field is not empty and the length is between 3-8 @NotEmpty(groups={}) @Size(min=3,max=8,groups={}) private String name; public String getName() { return name; } public void setName(String name) { = name; } public String getId() { return id; } public void setId(String id) { = id; } }
Control class:
package ; import ; import ; import ; import ; import ; import ; import ; import ; @Controller public class FirstController { @RequestMapping("/addPeople") //No need to verify the ID public @ResponseBody String addPeople(@Validated({}) People p,BindingResult result) { if(()) { return "0"; } return "1"; } }
3. Verify multiple objects
When processing multiple model objects on a functional method, multiple verification result objects need to be added.
package ; import ; import ; import ; import ; import ; import ; import ; @Controller public class FirstController { @RequestMapping("/addPeople") public @ResponseBody String addPeople(@Validated People p,BindingResult result,@Validated Person p2,BindingResult result2) { if(()) { return "0"; } if(()) { return "-1"; } return "1"; } }
Supplement: Main differences
When checking whether the controller's entry parameters comply with the specifications, there is not much difference in basic verification functions using @Validated or @Valid. However, there are two differences in functions such as grouping, annotation place, nesting verification:
@Validated | @Valid | |
Grouping | It provides grouping function, and when entering parameter verification, different verification mechanisms can be adopted according to different groups. | No grouping function |
Annotable location |
Can be used on types, methods and method parameters. But it cannot be used on member attributes | Can be used in methods, constructors, method parameters and member properties (whether they can be used to directly affect whether nested verification can be provided) |
Nested Verification |
Nested verification function cannot be provided separately when using method parameters. Cannot be used on member attributes. The framework is also not available for nested validation. Can be used for nested verification annotation @Valid for nested verification. |
Nested verification function cannot be provided separately when using method parameters. Can be used on member properties to prompt the verification framework for nested verification. Can be used for nested verification annotation @Valid for nested verification. |
Summarize
This is the article about the distinction summary of the spring annotation @Valid and @Validated. For more relevant content on the distinction between @Valid and @Validated, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!