java - JSR303 复合注解

标签 java bean-validation

我创建了一个由@Digits 和@Min 组成的复合注解

@Digits(integer=12, fraction=0)
@Min(value=0)
@ReportAsSingleViolation
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target( { FIELD, METHOD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Constraint(validatedBy={})
public @interface PositiveInt {
     String message() default "{positive.int.msg}";
     Class<?>[] groups() default {};
     Class<? extends Payload>[] payload() default {};
}

我的问题是,我想在使用 PositiveInteger 时指定 @Digits 'integer' 值的地方重用此注释

例子

public Demo{
    @PositiveInteger(integer=1)
    private Integer num1;

    @PositiveInteger(integer=2)
    private Integer num2;
}

其中 num1 可以是 1-9,num2 可以是 1-99。

这甚至可能吗?如果可以,我该怎么做?

目前,我必须提供一个自定义的 ConstraintValidator,我将在其中包含 @Digits 和 @Min 的验证码

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target( { FIELD, ANNOTATION_TYPE, PARAMETER })
@Constraint(validatedBy=PositiveIntConstraintValidator.class)
public @interface PositiveInt {
         String message() default "positive.int.msg";
     Class<?>[] groups() default {};
     Class<? extends Payload>[] payload() default {};

     int integer() default 1;
}


public class PositiveIntConstraintValidator implements ConstraintValidator<PositiveInt, Number> {
    private int maxDigits;

    @Override
    public void initialize(PositiveInt constraintAnnotation) {
    maxDigits = constraintAnnotation.integer();

    if (maxDigits < 1){
        throw new IllegalArgumentException("Invalid max size.  Max size must be a positive integer greater than 1");
    }
}


@Override
public boolean isValid(Number value, ConstraintValidatorContext context) {
    if (value == null){
        return true;
    }
    else if (value instanceof Long || value instanceof Integer || value instanceof Short || value instanceof BigInteger){
        String regex = "\\d{"0," + maxDigits + "}";
        return Pattern.matches(regex, value.toString());
    }
    return false;
}

最佳答案

您可以使用 @OverridesAnnotation :

@Digits(integer=0, fraction=0)
@Min(value=0)
@ReportAsSingleViolation
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target( { FIELD, METHOD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Constraint(validatedBy={})
public @interface PositiveInteger {
    String message() default "{positive.int.msg}";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};

    @OverridesAttribute(constraint=Digits.class, name="integer")
    int digits();
}

这样,@PositiveInteger#digits() 中给出的值将被传播到 @Digits

关于java - JSR303 复合注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13844161/

相关文章:

java - 字母数字字符串的@Pattern - Bean 验证

java - Android Studio - java.lang.NullPointerException : Attempt to invoke virtual method 'void android. widget.Button.setOnClickListener

java - 在类外覆盖 Object#equals(Object)

java - 当用户从图库上传图像并在textView中显示它们时,如何获取图像名称?

ajax - 如何强制执行 p :inputText value to be set through p:ajax if the JSR 303 validation constraint is violated?

hibernate - 当 Sessionfactory.getCurrentSession.merge 调用时,ConstraintValidator 中的 @Autowired bean null

jpa - 为什么 Bean 验证器在不可为空但自动生成的 id 字段上抛出 ConstraintViolationException?

java - 在方法中使用@AssertTrue 时,方法在验证(Bean Validation)期间被调用 4 次

java - 无法使用 API v2 Foreman 1.7.1 创建主机

java - Postgres 中字段 TIMESTAMP NOT TIME ZONE DEFAULT CURRENT_TIMESTAMP 的 JPA 模型类?