Java Reference
In-Depth Information
constraint includes the following steps: creating a constraint annotation and imple-
menting a validator.
Creating a constraint annotation
Although the goal is to create a constraint that ensures that a string character is cap-
italized, we will create a generic annotation. This annotation will take as parameter
the type of the expected case. Thus, it may, in the future, allow us to test if the char-
acters are uppercase or lowercase .
We will create the enumeration CaseType , which contains different types of case,
as shown in the following code:
public enum CaseType {
Once we have defined the possible types of cases, we will create our annotation
and define its characteristics directly. Already, it should be noted that in addition to
the basic features we've seen in the creation of annotations, you'll have to add the
@Constraint annotation that defines the validator of this constraint. For other fea-
tures, pleasereferto Chapter7 , Annotations and CDI .Thefollowing codeisthecode
of our annotation:
@Constraint(validatedBy = CaseValidator.class)
public @interface Case {
String message() default "This value must be
CaseType type() default CaseType.UPPER;
Class<? extends Payload>[] payload() default
Search WWH ::

Custom Search