user3332598
user3332598

Reputation: 95

Custom Validator and @interface List

I'm trying to do my first custom validator. I defined my annotation :

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = PojoValidator.class)
@Documented
public @interface Pojo {

  String message() default "invalid pojo";

  Class<?>[] groups() default {};

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

And in many tutorial on the web i see something like that at the end of the declaration :

@Target({ElementType.METHOD,
        ElementType.FIELD,
        ElementType.ANNOTATION_TYPE,
        ElementType.CONSTRUCTOR,
        ElementType.PARAMETER})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @interface List {
        Email[] value();
    }

What is the purpose of this @interface List, how should it be written in my example and is it mandatory?

Upvotes: 3

Views: 1514

Answers (1)

Joshua Hewitt
Joshua Hewitt

Reputation: 141

In Java 1.7- you can't have multiple annotations of the same type. The way round this is with List. Say you have a class-level validation to check that 2 fields are equal and you want to use it twice (eg to validate that passwd = repeatPasswd and email = repeatEmail:

@ValidateEquals.List({
    @ValidateEquals(field1 = "pwd", field2="repPwd"),
    @ValidateEquals(field1 = "email", field2="repEmail")
})
public class MyForm

Not sure if it is mandatory, but it is highly recommended for class-level validators.

Upvotes: 2

Related Questions