【Springboot之切面编程】自定义注解实现入参指定枚举值校验
通过自定义枚举注解
@EnumValidator(value = SexEnums.class),
private Integer sex;
就可以校验入参值必须在指定枚举类中
原创:https://www.jianshu.com/p/32f0d6e3afbb
目录
- 自定义枚举校验注解
- 使用枚举校验注解
- 演示
- 附
自定义枚举校验注解
1.定义自定义枚举@EnumValidator
package com.enums.validate.base;
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;
/**
* 枚举值校验注解
*
* @author: zetting
* @date:2018/12/18
*/
@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 {};
}
2.定义实现枚举值校验处理类EnumValidatorClass
package com.enums.validate.base;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* 枚举值校验注解实现
*
* @author: zetting
* @date:2018/12/18
*/
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.定义要校验的枚举值
package com.enums.validate.modules.enums;
/**
* 性别枚举
*
* @author: zetting
* @date: 2018/12/20 22:41
*/
public enum SexEnums {
/**
* 未知
*/
UNKNOWN(0),
/**
* 男
*/
MALE(1),
/**
* 女
*/
FEMALE(2);
private int value;
SexEnums(int value) {
this.value = value;
}
public int getValue() {
return value;
}}
2.在相应要检验的字段加上@EnumValidator注解
package com.enums.validate.modules.dto;
import com.enums.validate.aop.Request;
import com.enums.validate.base.EnumValidator;
import com.enums.validate.modules.enums.SexEnums;
import org.hibernate.validator.constraints.NotBlank;
/**
* 入参校验请求
*
* @author: zetting
* @date: 2018/12/19 22:16
*/
public class ValidateRequest extends Request {
/**
* 用户名
*/
@NotBlank
private String userName;
/**
* 性别
*/
@EnumValidator(value = SexEnums.class)
private Integer sex;
/**
* 地址
*/
private String address;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
2.controller层正常使用、无需添加任何东西
package com.enums.validate.modules.controller;
import com.enums.validate.base.Response;
import com.enums.validate.modules.dto.ValidateRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 枚举校验
*/
@RestController
public class MyController {
@GetMapping(value = "/validate")
public Response validate(ValidateRequest request) {
return Response.success();
}
}
演示
1.入参不再枚举值中
2.入参在枚举值中
附
gitee源码:
https://gitee.com/zetting/my-gather/tree/master/springboot-enums-validate