看一个例子:
@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消息的方法吗?
最佳答案
您需要将元注释@ReportAsSingleViolation
添加到CustomConstraint
的定义中。这样,任何对其组合约束的违反都将被报告为对组合约束的单一违反。另请参见Constraint Composition部分在 BV 引用中。
关于java - 如何在组合 JSR-303 注释中设置消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18356454/