看一个例子:
@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 消息的方式吗?