参考文档
系统执行业务逻辑之前,会对输入数据进行校验,检测数据是否有效合法的。所以我们可能会写大量的if else等判断逻辑,特别是在不同方法出现相同的数据时,校验的逻辑代码会反复出现,导致代码冗余,阅读性和可维护性极差。
hibernate-validator就提供了这套标准的实现,我们在用Springboot开发web应用时,会引入spring-boot-starter-web依赖,它默认会引入spring-boot-starter-validation依赖,而spring-boot-starter-validation中就引用了hibernate-validator依赖。
但是,在比较高版本的spring-boot-starter-web中,默认不再引用spring-boot-starter-validation,自然也就不会默认引入到hibernate-validator依赖,需要我们手动添加依赖。
<dependency>
<groupId>org.hibernate.validatorgroupId>
<artifactId>hibernate-validatorartifactId>
<version>6.1.7.Finalversion>
dependency>
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;
@Target(ElementType.FIELD) // 设置为使用在字段上
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Constraint(validatedBy = CheckParam.class) // 这里匹配的校验规则必须是参数化了当前校验注解并且实现了ConstraintValidator的类
public @interface Check {
// 字段对应的枚举
Class<? extends Enum<?>> enumClazz();
// 需要校验哪个字段属性
String filed();
// 校验不通过时的提示
String message() default "非法参数";
// 将validator进行分类,不同的类group中会执行不同的validator操作 (必须有这个属性)
Class<?>[] groups() default {};
// 主要是针对bean,很少使用(必须有这个属性)
Class<? extends Payload>[] payload() default {};
}
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
@Component
// 这里参数化两个参数,一个是这个校验规则对应的注解,以及校验的参的数类型,不指定的话则下面的实现方法为object
// 这里由于参数可能是数字也可能是string所以我使用了object
public class CheckParam implements ConstraintValidator<Check, Object> {
// 是否为必须参数
private boolean required;
private Class<? extends Enum<?>> enumClass;
private String filed;
// 参数校验初始化,默认无操作,这里我们就进行校验数据的初始化,参数为我们实例化的注解
@Override
public void initialize(Check constraintAnnotation) {
this.enumClass = constraintAnnotation.enumClazz();
this.filed = constraintAnnotation.filed();
}
@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
// 禁用默认响应消息
context.disableDefaultConstraintViolation();
if (StringUtils.isBlank(value.toString())) {
// 自定义响应内容
context.buildConstraintViolationWithTemplate("你不对劲").addConstraintViolation();
return false;
}
// 这里利用枚举校验攻击对枚举进行了校验。大家可以根基自己情况自行定制
if (!EnumCheck.valueBelongsToEnum(enumClass, filed, value.toString())) {
// 自定义响应内容
context.buildConstraintViolationWithTemplate("参数不在枚举范围").addConstraintViolation();
return false;
}
// 通过校验返回true
return true;
}
}
# Pojo
@Data
public class Goods {
private String goodsName;
// 指定了需要用哪个枚举规范来对比,对比枚举中的哪个属性
@Check(enumClazz = GoodsTypeEnum.class,filed = "type")
private Integer goodsType;
}
# Controller
@RestController
@RequestMapping("/enumCheck")
public class EnumCheckController {
@PostMapping("test")
public Object test(@RequestBody @Valid Goods goods){
return goods;
}
}
public class EnumCheck {
/**
* 判断参数是否属于某一个枚举clazz的某一个值fieldName
* @return
*/
public static boolean valueBelongsToEnum(Class<? extends Enum<?>> clazz, String fieldName,String val){
final Enum<?>[] enums = clazz.getEnumConstants();
if (null == enums) {
return false;
}
final List<Object> list = new ArrayList<>(enums.length);
for (Enum<?> e : enums) {
list.add(ReflectUtil.getFieldValue(e, fieldName));
}
// 这里进行转型后再比对,否则下面判定如果是String和integer进行判断始终为false;
final List<String> collect = list.stream().map(Object::toString).collect(Collectors.toList());
boolean contains = collect.contains(val);
return contains;
}
}
public enum GoodsTypeEnum {
PC("pc",1),
MOBILE("mobile",2),
WEB("web",3),
;
private final String info;
private final Integer type;
GoodsTypeEnum(String info, Integer type) {
this.info = info;
this.type = type;
}
public Integer getType() {
return type;
}
}
方法默认抛出MethodArgumentNotValidException
,我们在统一异常处理中捕获他进行处理,统一响应
@ExceptionHandler(MethodArgumentNotValidException.class)
public Result<Object> methodArgumentNotValidException(MethodArgumentNotValidException e){
log.error("参数校验异常 msg:{}",e.getMessage(),e );
String msg = e.getMessage();
String[] split = msg.split(";");
String temp = split[5].split("\\[")[1];
return Result.fail(50000,"["+split[0].split("'")[1]+"]"+temp.substring(0, temp.length()-3));
}