需求

自定义了如下枚举类型,需要对其vo进行校验:

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
26
27
public interface ValidateEnum {

/**
* 证件类型
*/
@Getter
@AllArgsConstructor
enum CertTypeEnum implements ValidateEnum {
CERT_TYPE("身份证","0"),
PROTECTION_TYPE("护照","1"),
OFFICER_TYPE("军官证","2"),
SOLDIERS_TYPE("士兵证","3"),
REENTRY_PERMIT_TYPE("回乡证","4"),
INTERIM_IDENTITY_CARD_TYPE("临时身份证","5"),
RESIDENCE_BOOKLET_TYPE("户口簿","6"),
POLICE_OFFICER_TYPE("警官证","7"),
TAIWAN_COMPATRIOTS_TYPE("台胞证","8"),
BUSINESS_LICENSE_TYPE("营业执照","9"),
OTHERS_TYPE("其他证件","10"),
HONG_KONG_TYPE("港澳台居民来往内地通行证","11"),
TAIWAN_TYPE("台湾居民来往大陆通行证","12");
private String name;
private String value;
}


}

实现

自定义枚举校验注解

1
2
3
4
5
6
7
8
9
10
11
12
13
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
@Constraint(validatedBy = EnumValidatorClass.class)
public @interface EnumValidator {
Class<?> value();

String message() default "入参值不在正确枚举中";

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

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

枚举注解处理类

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
26
27
28
29
30
31
32
33
34
35
public class EnumValidatorClass implements ConstraintValidator<EnumValidator, Object>, Annotation {
private Logger log = LoggerFactory.getLogger(this.getClass());
private List<Object> values = new ArrayList<>();

@Override
public void initialize(EnumValidator enumValidator) {
Class<?> clz = enumValidator.value();
Object[] ojects = clz.getEnumConstants();
try {
Method method = clz.getMethod("getValue");
if (Objects.isNull(method)) {
throw new Exception(String.format("枚举对象{}缺少字段名为value的字段",
clz.getName()));
}
Object value = null;
for (Object obj : ojects) {
value = method.invoke(obj);
values.add(value);
}
} catch (Exception e) {
log.error("[处理枚举校验异常]", e);
}
}


@Override
public Class<? extends Annotation> annotationType() {
return null;
}

@Override
public boolean isValid(Object value, ConstraintValidatorContext constraintValidatorContext) {
return Objects.isNull(value) || values.contains(value) ? true : false;
}
}

定义枚举类型接口

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
26
27
public interface ValidateEnum {

/**
* 证件类型
*/
@Getter
@AllArgsConstructor
enum CertTypeEnum implements ValidateEnum {
CERT_TYPE("身份证","0"),
PROTECTION_TYPE("护照","1"),
OFFICER_TYPE("军官证","2"),
SOLDIERS_TYPE("士兵证","3"),
REENTRY_PERMIT_TYPE("回乡证","4"),
INTERIM_IDENTITY_CARD_TYPE("临时身份证","5"),
RESIDENCE_BOOKLET_TYPE("户口簿","6"),
POLICE_OFFICER_TYPE("警官证","7"),
TAIWAN_COMPATRIOTS_TYPE("台胞证","8"),
BUSINESS_LICENSE_TYPE("营业执照","9"),
OTHERS_TYPE("其他证件","10"),
HONG_KONG_TYPE("港澳台居民来往内地通行证","11"),
TAIWAN_TYPE("台湾居民来往大陆通行证","12");
private String name;
private String value;
}


}

使用枚举校验注解

1
2
3
4
5
/**
* 证件类型(0:身份证, 1:护照, 2:军官证, 3:士兵证, 4:回乡证, 5:临时身份证, 6:户口簿, 7:警官证, 8:台胞证, 9:营业执照, 10:其他证件, 11:港澳台居民来往内地通行证, 12:台湾居民来往大陆通行证)
*/
@EnumValidator(message = "证件类型不在指定类型中", value = ValidateEnum.CertTypeEnum.class)
private String certType;

【1】简书主页·share猿
【2】掘金主页·share猿


扫描以下公众号关注小猿↓↓↓↓↓↓↓↓

更多资讯请在简书、微博、今日头条、掘金、CSDN都可以通过搜索“Share猿”找到小猿哦!!!