【发布时间】:2018-09-12 16:07:09
【问题描述】:
我使用 Spring 框架和 bean 验证规范(休眠实现)。考虑到下面的代码 sn-p,我使用@ReportAsSingleViolation 进行了以下观察:
添加
@ReportAsSingleViolation时仅返回默认消息。我的期望是触发失败的消息而不是默认消息。当我删除
@ReportAsSingleViolation注释时,一切都按预期工作。与违反的约束对应的所有错误消息都会正确返回。
这种行为是描述 Bean 验证规范的一部分还是一个实现问题?
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
//@ReportAsSingleViolation
@NotBlank(message = "The input cannot be blank")
@Pattern(regexp = "[0-9A-Z_]+", message = "The input must content only uppercase, numbers and undescore")
@Size(max = 30, message = "The input must be maximal 30")
@Documented
public @interface MyAnnotation{
String message() default "{com.example.MyAnnotation.message}";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
}
【问题讨论】:
标签: java bean-validation hibernate-validator spring-validator