gpt4 book ai didi

java - 如何强制 Hibernate Validator 递归(级联)验证字段?

转载 作者:行者123 更新时间:2023-11-30 06:56:51 26 4
gpt4 key购买 nike

我有以下模型:

class EmailWrapper {

@Email
private String subscriptionEmail;

private MyClass myClass;
//get and set
}


class MyClass {
@Size(min = 12)
private String str = "short";
//get and set
}

和以下代码:

EmailWrapper emailWrapper = new EmailWrapper();
emailWrapper.setSubscriptionEmail("qq@gmail.com");
emailWrapper.setMyClass(new MyClass());
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<EmailWrapper>> constraintViolations = validator.validate(emailWrapper);
System.out.println(constraintViolations.size());

如您所见,emailWrapper.myClass.str 违反了 @Size(min = 12)

但我在控制台中看到 0。如何解决这个问题?

最佳答案

替换

private MyClass myClass;

通过

@Valid
private MyClass myClass;

关于java - 如何强制 Hibernate Validator 递归(级联)验证字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34007784/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com