具有多个字段的Spring自定义注释验证


问题内容

这里有一个贪婪的小问题,希望这个问题也可以帮助其他想更多地了解注释验证的人

我目前正在学习Spring,现在,我计划尝试使用定制的带注释的验证。

我进行了很多搜索,现在我知道主要有两种验证方法,一种用于控制器,另一种是使用@Valid的注释方法

所以这是我的情况:假设我有两个或多个字段,当它们均为ALL
NULL时可以为null。但是,只有当这些字段之一包含空字符串以外的任何值时,才要求这些字段具有输入。我有两个想法,但不知道如何正确实施它们。

这是课程示例:

public class Subscriber {
    private String name;
    private String email;
    private Integer age;
    private String phone;
    private Gender gender;
    private Date birthday;
    private Date confirmBirthday;
    private String birthdayMessage;
    private Boolean receiveNewsletter;

    //Getter and Setter
}

假设我希望Birthday和confirmBirthday字段都必须为null或相反,我可能想对它们每个使用一个注释来对其进行注释,如下所示:

public class Subscriber {
    private String name;
    private String email;
    private Integer age;
    private String phone;
    private Gender gender;

    @NotNullIf(fieldName="confirmBirthday")
    private Date birthday;

    @NotNullIf(fieldName="birthday")
    private Date confirmBirthday;

    private String birthdayMessage;
    private Boolean receiveNewsletter;

    //Getter and Setter
}

所以我确实需要像这样创建验证注释:

@Documented
@Constraint(validatedBy = NotNullIfConstraintValidator.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.FIELD })
public @interface NotNullIf {

    String fieldName();

    String message() default "{NotNullIf.message}";
    Class<?>[] group() default {};
    Class<? extends Payload>[] payload() default {};
}

然后,我将需要创建验证器本身:

public class NotNullIfConstraintValidator implements ConstraintValidator<NotNullIf, String>{

    private String fieldName;

    public void initialize(NotNullIf constraintAnnotation) {
        fieldName = constraintAnnotation.fieldName();
    }

    public boolean isValid(String value, ConstraintValidatorContext context) {
        if(value == null) {
            return true;
        };
        //TODO Validation
        return false;
    }

}

那么如何实现呢?

对于使用同一类作为示例的另一个想法,例如说我想要生日,confirmBirthday和BirthdayMessdage只能为null或同时是相反的。这次我可能需要使用带注释的类进行跨字段验证。

这是我想对课程进行注释的方式:

@NotNullIf(fieldName={"birthday", "confirmBirthday", "birthdayMessage"})
public class Subscriber {
    //Those field same as the above one
}

因此,当该字段之一不为null时,还需要根据客户端大小输入其余字段。可能吗?

但是我仍然对上面列出的那些元素的注释验证如何工作感到困惑。也许我需要对该代码进行一些详细的说明,甚至更糟的是,我可能需要进行一些基本的概念检查。

请帮忙!


问题答案:

为此,只能使用类型级别注释,因为字段级别注释无法访问其他字段!

我做了类似的事情以允许选择验证(确切地说,多个属性之一必须不为null)。在您的情况下,@AllOrNone注释(或您希望使用的任何名称)将需要一个字段名称数组,并且您将带注释类型的整个对象提供给验证器:

@Target(ElementType.TYPE)
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = AllOrNoneValidator.class)
public @interface AllOrNone {
    String[] value();

    String message() default "{AllOrNone.message}";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}

public class AllOrNoneValidator implements ConstraintValidator<AllOrNone, Object> {
    private static final SpelExpressionParser PARSER = new SpelExpressionParser();
    private String[] fields;

    @Override
    public void initialize(AllOrNone constraintAnnotation) {
        fields = constraintAnnotation.value();
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        long notNull = Stream.of(fields)
                .map(field -> PARSER.parseExpression(field).getValue(value))
                .filter(Objects::nonNull)
                .count();
        return notNull == 0 || notNull == fields.length;
    }
}

(正如您所说,您使用Spring时,我使用SpEL甚至允许嵌套字段访问)

现在,您可以注释您的Subscriber类型:

@AllOrNone({"birthday", "confirmBirthday"})
public class Subscriber {
    private String name;
    private String email;
    private Integer age;
    private String phone;
    private Gender gender;
    private Date birthday;
    private Date confirmBirthday;
    private String birthdayMessage;
    private Boolean receiveNewsletter;
}