如何在Bean Validation 1.1 (JSR-349)的约束违反消息中显示字段名称描述自定义约束注释?
例如,给定以下自定义约束注释@Required
、资源包ValidationMessages.properties
和类Person
,我该如何编写约束违规消息“First Name is required.”(必填字段firstName
)和“Last Name is required.”(必填字段)姓氏
?
@Documented
@Constraint(validatedBy = {})
@Target({ ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
@NotEmpty
@ReportAsSingleViolation
public @interface Required {
public abstract Class<?>[] groups() default {};
public abstract String message() default "{Required.message}";
public abstract Class<? extends Payload>[] payload() default {};
}
在资源包中,ValidationMessages.properties
:
Required.message=is required.
类人
:
public class Person {
@Required
private String firstName;
@Required
private String lastName;
}
最佳答案
约束违反约束违反.. ((PathImpl)constraintViolation.getPropertyPath()).getLeafNode().getName();
关于java - 如何在自定义约束注解的约束违规消息中显示字段名称描述?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29827436/