简体   繁体   中英

Custom Constraint Validator Annotation

I'm trying to create a custom constraint validator annotation. This is my annotation definition below. Eclipse complains that the "Target annotation is disallowed for this location." The same goes for Retention and Constraint. I'm using Java 1.7

package com.test;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;

import java.lang.annotation.Target;
import java.lang.annotation.Retention;
import javax.validation.Constraint;
import javax.validation.Payload;

@Target(PARAMETER)
@Retention(RUNTIME)
@Constraint(validatedBy = MyValidator.class)
public interface MyValidationAnnotation{

    String message() ;

    Class<?>[] groups() ;

    Class<? extends Payload>[] payload() ;
}

Change public interface to public @interface .

Creating Your Own Annotations

It is possible to create your own (custom) Java annotations. Annotations are defined in their own file, just like a Java class or interface. Here is custom Java annotation example:

 @interface MyAnnotation {

    String   value();

    String   name();
    int      age();
    String[] newNames();

}

This example defines an annotation called MyAnnotation which has four elements. Notice the @interface keyword. This signals to the Java compiler that this is a Java annotation definition.

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