自定义约束验证器注解

Custom Constraint Validator Annotation

我正在尝试创建自定义约束验证器注释。这是我下面的注释定义。 Eclipse 抱怨 "Target annotation is disallowed for this location." Retention 和 Constraint 也是如此。我正在使用 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() ;
}

public interface 更改为 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.