How do I display the field name description in the constraint violation message of a Bean Validation 1.1 (JSR-349) custom constraint annotation?
For example, given the following custom constraint annotation @Required
, resource bundle ValidationMessages.properties
, and class Person
, how can I compose the constraint violation message "First Name is required." for required field firstName
and "Last Name is required." for required field lastName
?
@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 {};
}
In resource bundle, ValidationMessages.properties
:
Required.message=is required.
Class Person
:
public class Person {
@Required
private String firstName;
@Required
private String lastName;
}