user994165
user994165

Reputation: 9516

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() ;
}

Upvotes: 1

Views: 1962

Answers (1)

Keith
Keith

Reputation: 3151

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.

Upvotes: 5

Related Questions