java – JSR 303自定义约束覆盖

我想在字段(本例中是字符串)上放置一组标准约束(如非空字母数字字符串,长度为3到240个字符),并想知道是否有一种方法可以覆盖模型代码中的某些约束.这也将是一个重写,或者只是对覆盖的注释进行两次验证?

它应该是这样的

@AlphanumericString
@Size(min=100,max=150) //override standart values from AlphanumericString annotation

谢谢你的回答

好的,回答我自己. @OverridesParameter有助于重新分配嵌套的注释参数

@Numerical
@Size //arbitrary parameter values
@ConstraintValidator(frenchZipcodeValidator.class)
@Documented
@Target({ANNOTATION_TYPE,METHOD,FIELD})
@Retention(RUNTIME)
public @interface frenchZipCode {
    String message() default "Wrong zipcode";
    String[] groups() default {};

    @OverridesParameters( {
        @OverridesParameter(constraint=Size.class,parameter="min")
        @OverridesParameter(constraint=Size.class,parameter="max") } )
    int size() default 5;

    @OverridesParameter(constraint=Size.class,parameter="message")
    String sizeMessage() default "{error.zipcode.size}";

    @OverridesParameter(constraint=Numerical.class,parameter="message")
    String numericalMessage() default "{error.zipcode.numerical}";
}

source

解决方法

这是一个很好的问题. JSR 303 Bean Validation specification描述了3.5节中的验证例程.

For a given group to validate,the validation routine applied on a
given bean instance is expected to execute the following constraint
validations in no particular order:

  • for all reachable fields,execute all field level validations (including the ones expressed on superclasses) matching the targeted
    group unless the given validation constraint has already been
    processed during this validation routine for a given navigation path
    (see Section 3.5.1) as part of a prevIoUs group match.

The object validation routine is described as such. For each
constraint declaration:

  • determine for the constraint declaration,the appropriate ConstraintValidator to use (see Section 3.5.3).
  • execute the isValid operation (from the constraint validation implementation) on the appropriate data (see Section 2.4)
  • if isValid returns true,continue to the next constraint,
  • if isValid returns false,the Bean Validation provider populates ConstraintViolation object(s) according to the rules defined in Section 2.4 and appends these objects to the list of constraint violations.

在您的情况下,您将处理目标组为Default的简单String字段的验证.您有两个验证约束(@AlphanumericString和@Size),根据文档,它们将按照特定顺序单独验证/处理.

所以回答你的问题.不,当您使用@Size addaly时,@ AlphanumericString不会应用任何覆盖.为了能够实现我认为您尝试执行的操作,您可以创建一个约束组合,其中您可以从组成注释中覆盖属性

@Pattern(regexp="[a-zA-Z]*")
@Size
@Constraint(validatedBy = AlphanumericStringValidator.class)
@Documented
@Target({ METHOD,FIELD,ANNOTATION_TYPE,CONSTRUCTOR,ParaMETER })
@Retention(RUNTIME)
public @interface AlphanumericString {
   // ...
  @OverridesAttribute(constraint=Size.class,name="min")
  int min() default 3
  @OverridesAttribute(constraint=Size.class,name="max")
  int max() default 230;       
   // ...
}

并使用它:

@AlphanumericString(min = 100,max = 150)

相关文章

Java中的String是不可变对象 在面向对象及函数编程语言中,不...
String, StringBuffer 和 StringBuilder 可变性 String不可变...
序列化:把对象转换为字节序列的过程称为对象的序列化. 反序...
先说结论,是对象!可以继续往下看 数组是不是对象 什么是对...
为什么浮点数 float 或 double 运算的时候会有精度丢失的风险...
面试题引入 这里引申出一个经典问题,看下面代码 Integer a ...