Bean验证组-验证内部类

时间:2018-10-29 11:50:48

标签: java validation

我试图通过此代码仅在ValidateZZ.class组中验证B.name

@GroupSequence({A.class, ValidateZZ.class})
class A {
    @NotNull(group = ValidateZZ.class)
    String name;

    @Valid
    B classB;
}

@Validate
class B {
    @NotNull(group = ValidateZZ.class)
    String name;
}

但这是行不通的! A.name验证正确,但a.classB.name未验证。 显然我做错了。我该如何解决?

1 个答案:

答案 0 :(得分:0)

我以这种方式解决了这个问题:

private <T> void validateFormData(T formData, Class<T> clazz, LoanApplicationIo loanApplication) {
    List<Class> groups = Arrays.stream(clazz.getAnnotations())
        .filter(annotation -> annotation instanceof GroupSequence)
        .flatMap(annotation -> Arrays.stream(((GroupSequence)annotation).value()))
        .filter(Class::isInterface)
        .distinct()
        .collect(Collectors.toList());
    if (!groups.contains(Default.class)) {
        groups.add(Default.class);
    }

    Set<ConstraintViolation<T>> violations = validator.validate(formData, groups.toArray(new Class[groups.size()]));

我不知道正确的方法,但是可以。