请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

使用组序列验证时不会验证默认组吗?

老师您好,我看视频中提到,如果有约束不明确定义属于哪个组,则属于默认组,但我使用下来发现用组序列验证时并不会去验证那些没有分组的属性呀。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package vaildation;
 
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Length;
 
import javax.validation.*;
import javax.validation.constraints.*;
import javax.validation.groups.Default;
import java.util.Date;
import java.util.List;
import java.util.Set;
 
/*
 * 待验证对象实体类
 * 用户信息类
 * */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserInfo
{
    //登陆场景
    public interface LoginGroup
    {
    }
 
    //注册场景
    public interface RegisterGroup
    {
    }
 
    //组排序场景
    @GroupSequence({
            LoginGroup.class,
            RegisterGroup.class,
            Default.class
    })
    public interface Group
    {
 
    }
 
    @NotNull(message = "用户ID不能为空", groups = LoginGroup.class)
    private String userId;//用户ID
    private String userName;//用户名
    @Length(min = 5,max = 10,message = "密码长度要大于等于5位,小于等于10位")
    private String passWord;//用户密码
    @NotNull(message = "邮箱不能为空", groups = RegisterGroup.class)
    @Email(message = "邮箱必须为有效邮箱")
    private String email;//邮箱
    private String phone;//手机号
    @Min(value = 18, message = "年龄不能小于18岁")
    @Max(value = 60, message = "年龄不能大于60岁")
    private Integer age;//年龄
    @Past(message = "生日不能是未来时间点")
    private Date birthday;//生日
    @Size(min = 1,message = "不能少于1个好友")
    private List<@Valid UserInfo> friends;//好友列表
     
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package vaildation;
 
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
 
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Set;
 
/*
* 验证测试类
* */
public class ValidationTest
{
    //验证器对象
    private Validator validator;
 
    //待验证对象
    private UserInfo userInfo;
 
    //验证结果集合
    private Set<ConstraintViolation<UserInfo>> set;
 
    /*
    * 初始化操作
    * */
    @Before
    public void init()
    {
        //初始化验证器
        validator = Validation.buildDefaultValidatorFactory().getValidator();
 
        //初始化待验证对象:用户信息
        userInfo = new UserInfo();
        userInfo.setUserId("007");
        userInfo.setPassWord("1");//此处密码是不符合约束的
        userInfo.setAge(99);//此处age也是不符合约束的
 
    }
 
    /*
     * 结果打印
     * */
    @After
    public void print()
    {
        set.forEach(item -> System.out.println(item.getMessage()));
    }
 
   /* @Test
    public void nullValidation()
    {
        //使用验证器对对象进行验证
       set = validator.validate(userInfo);
    }
 
    *//*
    * 分组验证
    * *//*
    @Test
    public void groupValidation()
    {
        set = validator.validate(userInfo, UserInfo.LoginGroup.class);
    }*/
 
 
    @Test
    public void groupSequenceValidation()
    {
        //组序列验证时不会验证年龄和密码
        set = validator.validate(userInfo, UserInfo.Group.class);
         
        //单独测下面这个则会发现密码和年龄约束都是起作用的。
//        set = validator.validate(userInfo);
    }
}


正在回答 回答被采纳积分+3

插入代码

1回答

张小喜 2020-04-26 22:58:33

对的,当你设置的自定义校验组之后,默认的就不管用了。我印象中好像强调过。如果有这种需求,可以自定义一个默认组来实现。

1 回复 有任何疑惑可以回复我~
  • 提问者 乃好 #1
    ?那为啥序列组里面还要整个默认组
    回复 有任何疑惑可以回复我~ 2020-04-26 23:01:47
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号