Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JSR-303 Bean验证-自定义约束一个验证器的多个注释_Java_Bean Validation - Fatal编程技术网

Java JSR-303 Bean验证-自定义约束一个验证器的多个注释

Java JSR-303 Bean验证-自定义约束一个验证器的多个注释,java,bean-validation,Java,Bean Validation,编写自定义约束时,一个验证器实现可以验证多个注释。例如,我有几个注释,它们规定了不同的@size注释,但我希望它们都指向同一个验证器类,该类执行一些全局检查,即所有注释都必须与某个正则表达式匹配。就我所见,实现采用了一种注释类型 一个注释 @Target( { METHOD, FIELD, ANNOTATION_TYPE, TYPE}) @Retention(RetentionPolicy.RUNTIME) @Constraint(validatedBy = {UCNValidator.clas

编写自定义约束时,一个验证器实现可以验证多个注释。例如,我有几个注释,它们规定了不同的@size注释,但我希望它们都指向同一个验证器类,该类执行一些全局检查,即所有注释都必须与某个正则表达式匹配。就我所见,实现采用了一种注释类型

一个注释

@Target( { METHOD, FIELD, ANNOTATION_TYPE, TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {UCNValidator.class})
@Documented
@Size(min = 9, max = 9, message = "{exactlength}")
public @interface UCN {

    String message() default "{invalidFormat}";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

    String fieldName() default "ucn";

}
@Target({METHOD,FIELD,ANNOTATION\u TYPE,TYPE})
@保留(RetentionPolicy.RUNTIME)
@约束(validatedBy={UCNValidator.class})
@记录
@大小(最小值=9,最大值=9,消息=“{exactlength}”)
公共@接口UCN{
字符串消息()默认值“{invalidFormat}”;
类[]组()默认值{};

类在验证某个对象的属性时,似乎没有办法从该对象访问其他值。我使用的解决方案是将注释放在该类上,然后验证器将获取整个对象进行验证,您可以访问执行验证所需的信息

我写了一篇文章来比较一个对象的两个不同属性:

@Target(TYPE)
@Retention(RUNTIME)
@Constraint(validatedBy = LessThanValidator.class)
@Documented
public @interface LessThan {

    String message() default "{com.bullethq.constraints.LessThan}";

    String bigValueProperty();

    String littleValueProperty();

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};
}
@Target(类型)
@保留(运行时)
@约束(validatedBy=LessThanValidator.class)
@记录
公共@interface LessThan{
字符串消息()默认值为“{com.bullethq.constraints.LessThan}”;
字符串bigValueProperty();
字符串littleValueProperty();
类[]组()默认值{};

ClassDecision无论如何这不是最好的方法。对于常见的验证功能,我可以编写一个供所有验证程序使用的通用方法。如果您可以发布
getValue
@Target(TYPE)
@Retention(RUNTIME)
@Constraint(validatedBy = LessThanValidator.class)
@Documented
public @interface LessThan {

    String message() default "{com.bullethq.constraints.LessThan}";

    String bigValueProperty();

    String littleValueProperty();

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};
}
public class LessThanValidator implements ConstraintValidator<LessThan, Object> {

    private LessThan constraint;

    public void initialize(LessThan constraintAnnotation) {
        constraint = constraintAnnotation;
    }

    public boolean isValid(Object object, ConstraintValidatorContext cvc) {
        Object bigValue = getValue(object, constraint.bigValueProperty());
        Object littleValue = getValue(object, constraint.littleValueProperty());

        // If one of the values is null, then we do not perform validation.
        if (bigValue == null || littleValue == null) {
            return true;
        }

        if (bigValue instanceof Comparable && littleValue instanceof Comparable) {
            boolean valid = ((Comparable<Object>) bigValue).compareTo(littleValue) > 0;
            if (!valid) {
                // If the values are not valid, then build a custom violations which has the correct path in it.
                cvc.buildConstraintViolationWithTemplate(cvc.getDefaultConstraintMessageTemplate())
                        .addNode(constraint.littleValueProperty())
                        .addConstraintViolation().disableDefaultConstraintViolation();
            }
            return valid;
        }
        throw new IllegalArgumentException("Properties " + constraint.bigValueProperty() + " and " + constraint.littleValueProperty() + " both need to be comparable in " + object.getClass());
    }
}