@Size(min = 1, message = “不能少于1个好友”)
private List<@Valid UserInfo> friends;
public void init() {
validator = Validation.buildDefaultValidatorFactory().getValidator(); //初始化验证器
userInfo = new UserInfo(); //初始化待验证对象
userInfo.setId("1");
userInfo.setUsername("zzz");
userInfo.setPassword("zzzzzzz");
userInfo.setEmail("88888@qq.com");
userInfo.setAge(18);
Calendar calendar = Calendar.getInstance();
calendar.set(2020, Calendar.FEBRUARY, 1);
userInfo.setBirthday(calendar.getTime());
UserInfo friend = new UserInfo();
friend.setId("2");
friend.setUsername("xxx");
friend.setPassword("xxxxxxx");
friend.setEmail("99999@qq.com");
friend.setAge(55);
List<UserInfo> friends = new ArrayList<>();
friends.add(friend);
userInfo.setFriends(friends);
}
控制台打印
十二月 08, 2020 8:09:02 下午 org.hibernate.validator.internal.util.Version <clinit>
INFO: HV000001: Hibernate Validator 6.0.16.Final
UserInfo(id=1, username=zzz, password=zzzzzzz, email=88888@qq.com, phone=null, age=18, birthday=Sat Feb 01 20:09:02 CST 2020, friends=[UserInfo(id=2, username=xxx, password=xxxxxxx, email=99999@qq.com, phone=null, age=55, birthday=null, friends=null)])
进程已结束,退出代码 0
测试时,集合中只有一个对象,而集合中这个对象的friends集合并没有初始化也没有填充
为什么级联验证时,没有再次验证集合中friend这个实例的friends集合属性