2

看一个例子:

@Pattern(regexp="[0-9]*")
@Size(max =5)
@Documented
@Target({ANNOTATION_TYPE, METHOD, FIELD, CONSTRUCTOR, PARAMETER})
@Retention(RUNTIME)
@Constraint(validatedBy = {}) //do not want any programmatic validation
public @interface CustomAnnotation {
    String message() default "";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
    }
}

带注释字段的示例:

@CustomAnnotation(message = "some important message")
private String field;

当字段违反@Pattern 时,我收到来自@Pattern 而不是来自@CustomAnnotation 的错误消息。这是只显示@CustomAnnotation 消息的方式吗?

4

1 回答 1

2

您需要将元注释添加@ReportAsSingleViolationCustomConstraint. 这样,任何违反其组成约束之一的行为都将被报告为对组成约束的单一违反。另请参见BV 参考中的约束组合部分。

于 2013-08-21T13:26:28.057 回答