Search code examples
javavalidationcustomvalidator

Custom Class Level Validation - Creating the Annotation


I want to implement a custom validator in a Spring Boot v1.5.14.RELEASE app. First I create a custom constraint annotation:

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public interface CreateBook {
}

However, I receive this compilation error:

@Retention not applicable to type

Solution

  • The problem is with how you define a new annotation. It should be like this:

    @Retention(RetentionPolicy.RUNTIME)
    public @interface CreateBook {
    }
    

    Note the @ character in @interface