In fact ,if you want to control the size of list ,you may use the @Size on you parameter, some @valid annotation has support List.
But if you want to make some complicated function, i suggest you implement your own validation annotation by the support of @Valid.
Follow i will show you a simple implement:
import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.util.HashSet;
import java.util.Set;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* @author yujin
*/
@Documented
@Constraint(validatedBy = {Show.ShowConstraintValidator.class})
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RUNTIME)
public @interface Show {
String message() default "{com.annotation.Show.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
int[] value();
class ShowConstraintValidator implements ConstraintValidator<Show, Integer> {
private Set<Integer> set = new HashSet<>();
@Override
public void initialize(Show constraintAnnotation) {
int[] value = constraintAnnotation.value();
for (int i : value) {
set.add(i);
}
}
@Override
public boolean isValid(Integer value, ConstraintValidatorContext context) {
return set.contains(value);
}
}
}
The dependency is :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>