Creating a simple constraint - The constraint annotation
This topic has not yet been written. The content below is from the topic description.
3.1.1. The constraint annotation Let's write a constraint annotation, that can be used to express that a given string shall either be upper case or lower case. We'll apply it later on to the licensePlate field of the Car class from Chapter 1, Getting started to ensure, that the field is always an upper-case string. First we need a way to express the two case modes. We might use String constants, but a better way to go is to use a Java 5 enum for that purpose: Example 3.1. Enum CaseMode to express upper vs. lower case package com.mycompany; public enum CaseMode { UPPER, LOWER; } Now we can define the actual constraint annotation. If you've never designed an annotation before, this may look a bit scary, but actually it's not that hard: Example 3.2. Defining CheckCase constraint annotation package com.mycompany; import static java.lang.annotation.ElementType.*; import static java.lang.annotation.RetentionPolicy.*; import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.Target; import javax.validation.Constraint; import javax.validation.Payload; @Target( { METHOD, FIELD, ANNOTATION_TYPE }) @Retention(RUNTIME) @Constraint(validatedBy = CheckCaseValidator.class) @Documented public @interface CheckCase { String message() default "{com.mycompany.constraints.checkcase}"; Class [] groups() default {}; Class [] payload() default {}; CaseMode value(); } An annotation type is defined using the @interface keyword. All attributes of an annotation type are declared in a method-like manner. The specification of the Bean Validation API demands, that any constraint annotation defines an attribute message that returns the default key for creating error messages in case the constraint is violated an attribute groups that allows the specification of validation groups, to which this constraint belongs (see Section 2.3, “Validating groups”). This must default to an empty array of type Class . an attribute payload that can be used by clients of the Bean Validation API to assign custom payload objects to a constraint. This attribute is not used by the API itself. Tip An example for a custom payload could be the definition of a severity. public class Severity { public static class Info extends Payload {}; public static class Error extends Payload {}; } public class ContactDetails { @NotNull(message="Name is mandatory", payload=Severity.Error.class) private String name; @NotNull(message="Phone number not specified, but not mandatory", payload=Severity.Info.class) private String phoneNumber; // ... } Now a client can after the validation of a ContactDetails instance access the severity of a constraint using ConstraintViolation.getConstraintDescriptor().getPayload() and adjust its behaviour depending on the severity. Besides those three mandatory attributes (message, groups and payload) we add another one allowing for the required case mode to be specified. The name value is a special one, which can be omitted upon using the annotation, if it is the only attribute specified, as e.g. in @CheckCase(CaseMode.UPPER). In addition we annotate the annotation type with a couple of so-called meta annotations: @Target({ METHOD, FIELD, ANNOTATION_TYPE }): Says, that methods, fields and annotation declarations may be annotated with @CheckCase (but not type declarations e.g.) @Retention(RUNTIME): Specifies, that annotations of this type will be available at runtime by the means of reflection @Constraint(validatedBy = CheckCaseValidator.class): Specifies the validator to be used to validate elements annotated with @CheckCase @Documented: Says, that the use of @CheckCase will be contained in the JavaDoc of elements annotated with it