简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM