一个约束注解可以关联多个验证器,根据要验证的属性类型选择合适的验证器,及代码中可能有如下写法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = {AgeIntegerValidtor.class, AgeStringValidtor.class})
public @interface Age {
}
public class AgeIntegerValidtor implements ConstraintValidator<Age, Integer> {
@Override
public boolean isValid(Integer value, ConstraintValidatorContext context) {
}
}
public class AgeStringValidtor implements ConstraintValidator<Age, Integer> {
@Override
public boolean isValid(Integer value, ConstraintValidatorContext context) {
}
}
|
之所以单独提一下这件事,是因为这件事我之前从未注意到。