加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – JSR 303自定义约束覆盖

发布时间:2020-12-15 00:49:33 所属栏目:Java 来源:网络整理
导读:我想在字段(本例中是字符串)上放置一组标准约束(如非空字母数字字符串,长度为3到240个字符),并想知道是否有一种方法可以覆盖模型代码中的某些约束.这也将是一个重写,或者只是对覆盖的注释进行两次验证? 它应该是这样的 @AlphanumericString@Size(min=100,ma
我想在字段(本例中是字符串)上放置一组标准约束(如非空字母数字字符串,长度为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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读