JSR303 data check group check

JSR303 data check group check

one example:

public class Manager{
    
    
private static final long serialVersionUID = 48L;

    private Integer id;
    @NotBlank(message = "用户名不允许为空",groups = {
    
    AddEmployee.class,Login.class})
    @Length(min = 4, max = 25, message = "用户名长度必须在4~25个字符之间",groups = {
    
    AddEmployee.class, Login.class})
    private String name;
    @NotBlank(message = "密码不允许为空",groups = {
    
    AddEmployee.class, Login.class})
    @Length(min = 4, max = 25, message = "密码长度必须在4~25个字符之间",groups = {
    
    AddEmployee.class, Login.class})
    private String pass;

    @NotNull(message = "员工工资不能为空", groups = {
    
    AddEmployee.class})
    @Range(min = 3000, max = 6000, message = "员工工资必须在3000~6000之间",groups = AddEmployee.class)
    private double salary;

    /**
     * 上面定义的校验规则分为两组:
     * 1. 用户登录时的校验规则:需要对name和pass两个成员变量进行数据校验.
     * 2. 添加员工是的校验规则:需要对name、pass和salary进行数据校验。
     */
......
}

Analysis:

The verification rules defined above are divided into two groups:

  • The verification rule when the user logs in: data verification is required for the two member variables of name and pass.
  • Add verification rules for employee yes: data verification for name, pass, and salary is required.

The annotation groups attribute located before the name and pass member variables in the above code specify the AddEmployee and Login two groups, which indicates that the data verification of the name and pass member variables is performed in these two groups; and the one located before the salary member variable The groups attribute of the annotation only specifies an AddEmployee group, which means that the data verification of salary member variables is only performed in the AddEmployee group.

The two groups AddEmployee and Login are actually a simple identification. Just quantify the corresponding interfaces for them, which can be empty interfaces.

Check in the controller:

@PostMapping("/processLogin")
    public String processLogin(@Validated(Login.class) Manager manager,
                               BindingResult bindingResult, String vercode,
                               RedirectAttributes attrs, WebRequest webRequest){
    
    ...}

The above Manager parameter is modified with the @Validated(Login.class) annotation, and the value attribute of this annotation is assigned Login.class-the value of this attribute is the group name for group verification.

Comparing the grouping of the verification annotations of the Manager class above, this means that the two variables of name and pass will perform data verification under the Login and AddEmployee groups; while the salary member variable will only perform data verification under the AddEmployee group.

The above processing method specifies the value attribute value of @Validated as Login.class, which means that the verification group is Login, so data verification will only be performed on the name and pass variables of the Manager parameter.

Guess you like

Origin blog.csdn.net/qq_40596572/article/details/115220166