SpringBoot Validation参数校验 详解自定

前言

Hibernate Validator 是 Bean Validation 的参考实现 。Hibernate Validator 提供了 JSR 303 规范中所有内置 constraint 的实现,除此之外还有一些附加的 constraint
在日常开发中,Hibernate Validator经常用来验证bean的字段,基于注解,方便快捷高效。

在SpringBoot中可以使用@Validated,注解Hibernate Validator加强版,也可以使用@Valid原来Bean Validation java版本

内置校验注解

Bean Validation 中内置的 constraint

注解 作用
@Valid 被注释的元素是一个对象,需要检查此对象的所有字段值
@Null 被注释的元素必须为 null
@NotNull 被注释的元素必须不为 null
@AssertTrue 被注释的元素必须为 true
@AssertFalse 被注释的元素必须为 false
@Min(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@Max(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@DecimalMin(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@DecimalMax(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@Size(max, min) 被注释的元素的大小必须在指定的范围内
@Digits (integer, fraction) 被注释的元素必须是一个数字,其值必须在可接受的范围内
@Past 被注释的元素必须是一个过去的日期
@Future 被注释的元素必须是一个将来的日期
@Pattern(value) 被注释的元素必须符合指定的正则表达式

Hibernate Validator 附加的 constraint

注解 作用
@Email 被注释的元素必须是电子邮箱地址
@Length(min=, max=) 被注释的字符串的大小必须在指定的范围内
@NotEmpty 被注释的字符串的必须非空
@Range(min=, max=) 被注释的元素必须在合适的范围内
@NotBlank 被注释的字符串的必须非空
@URL(protocol=,host=, port=, regexp=, flags=) 被注释的字符串必须是一个有效的url
@CreditCardNumber 被注释的字符串必须通过Luhn校验算法,银行卡,信用卡等号码一般都用Luhn计算合法性
@ScriptAssert(lang=, script=, alias=) 要有Java Scripting API 即JSR 223(“Scripting for the JavaTM Platform”)的实现
@SafeHtml(whitelistType=,additionalTags=) classpath中要有jsoup包

message支持表达式和EL表达式 ,比如message = “姓名长度限制为{min}到{max} ${1+2}”)

想把错误描述统一写到properties的话,在classpath下面新建ValidationMessages_zh_CN.properties文件(注意value需要转换为unicode编码),然后用{}格式的占位符

hibernate补充的注解中,最后3个不常用,可忽略。
主要区分下@NotNull @NotEmpty @NotBlank 3个注解的区别:

  1. @NotNull 任何对象的value不能为null
  2. @NotEmpty 集合对象的元素不为0,即集合不为空,也可以用于字符串不为null
  3. @NotBlank 只能用于字符串不为null,并且字符串trim()以后length要大于0

分组校验

如果同一个参数,需要在不同场景下应用不同的校验规则,就需要用到分组校验了。比如:新注册用户还没起名字,我们允许name字段为空,但是在更新时候不允许将名字更新为空字符。

分组校验有三个步骤:

  1. 定义一个分组类(或接口)
1
2
java复制代码public interface Update extends Default{
}
  1. 在校验注解上添加groups属性指定分组
1
2
3
4
5
java复制代码public class UserVO {
@NotBlank(message = "name 不能为空",groups = Update.class)
private String name;
// 省略其他代码...
}
  1. Controller方法的@Validated注解添加分组类
1
2
3
4
java复制代码@PostMapping("update")
public ResultInfo update(@Validated({Update.class}) UserVO userVO) {
return new ResultInfo().success(userVO);
}

自定义的Update分组接口继承了Default接口。校验注解(如: @NotBlank)和@validated默认其他注解都属于Default.class分组,这一点在javax.validation.groups.Default注释中有说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
java复制代码/**
* Default Jakarta Bean Validation group.
* <p>
* Unless a list of groups is explicitly defined:
* <ul>
* <li>constraints belong to the {@code Default} group</li>
* <li>validation applies to the {@code Default} group</li>
* </ul>
* Most structural constraints should belong to the default group.
*
* @author Emmanuel Bernard
*/
public interface Default {
}

在编写Update分组接口时,如果继承了Default,下面两个写法就是等效的:
@Validated({Update.class}),@Validated({Update.class,Default.class})
如果Update不继承Default,@Validated({Update.class})就只会校验属于Update.class分组的参数字段

递归校验

如果 UserVO 类中增加一个 OrderVO 类的属性,而 OrderVO 中的属性也需要校验,就用到递归校验了,只要在相应属性上增加@Valid注解即可实现(对于集合同样适用)

1
2
3
4
5
6
7
java复制代码public class OrderVO {
@NotNull
private Long id;
@NotBlank(message = "itemName 不能为空")
private String itemName;
// 省略其他代码...
}
1
2
3
4
5
6
7
8
java复制代码public class UserVO {
@NotBlank(message = "name 不能为空",groups = Update.class)
private String name;
//需要递归校验的OrderVO
@Valid
private OrderVO orderVO;
// 省略其他代码...
}

自定义校验

validation 为我们提供了这么多特性,几乎可以满足日常开发中绝大多数参数校验场景了。但是,一个好的框架一定是方便扩展的。有了扩展能力,就能应对更多复杂的业务场景,毕竟在开发过程中,唯一不变的就是变化本身。 Validation允许用户自定义校验

实现很简单,分两步:

  1. 自定义校验注解
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
java复制代码package cn.soboys.core.validator;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;


/**
* @author kenx
* @version 1.0
* @date 2021/1/21 20:49
* 日期验证 约束注解类
*/
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Constraint(validatedBy = {IsDateTimeValidator.class}) // 标明由哪个类执行校验逻辑
public @interface IsDateTime {

// 校验出错时默认返回的消息
String message() default "日期格式错误";
//分组校验
Class<?>[] groups() default {};

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

//下面是我自己定义属性
boolean required() default true;

String dateFormat() default "yyyy-MM-dd";


}

注意:message用于显示错误信息这个字段是必须的,groups和payload也是必须的
@Constraint(validatedBy = { HandsomeBoyValidator.class})用来指定处理这个注解逻辑的类

  1. 编写校验者类
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
36
37
38
39
40
41
42
43
44
45
46
47
java复制代码package cn.soboys.core.validator;

import cn.hutool.core.util.StrUtil;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
* @author kenx
* @version 1.0
* @date 2021/1/21 20:51
* 日期验证器
*/
public class IsDateTimeValidator implements ConstraintValidator<IsDateTime, String> {

private boolean required = false;
private String dateFormat = "yyyy-MM-dd";

/**
* 用于初始化注解上的值到这个validator
* @param constraintAnnotation
*/
@Override
public void initialize(IsDateTime constraintAnnotation) {
required = constraintAnnotation.required();
dateFormat = constraintAnnotation.dateFormat();
}

/**
* 具体的校验逻辑
* @param value
* @param context
* @return
*/
public boolean isValid(String value, ConstraintValidatorContext context) {
if (required) {
return ValidatorUtil.isDateTime(value, dateFormat);
} else {
if (StrUtil.isBlank(value)) {
return true;
} else {
return ValidatorUtil.isDateTime(value, dateFormat);
}
}
}

}

注意这里验证逻辑我抽出来单独写了一个工具类,ValidatorUtil

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
java复制代码package cn.soboys.core.validator;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author kenx
* @version 1.0
* @date 2021/1/21 20:51
* 验证表达式
*/
public class ValidatorUtil {
private static final Pattern mobile_pattern = Pattern.compile("1\\d{10}");
private static final Pattern money_pattern = Pattern.compile("^[0-9]+\\.?[0-9]{0,2}$");

/**
* 验证手机号
*
* @param src
* @return
*/
public static boolean isMobile(String src) {
if (StrUtil.isBlank(src)) {
return false;
}
Matcher m = mobile_pattern.matcher(src);
return m.matches();
}


/**
* 验证枚举值是否合法 ,所有枚举需要继承此方法重写
*
* @param beanClass 枚举类
* @param status 对应code
* @return
* @throws Exception
*/
public static boolean isEnum(Class<?> beanClass, String status) throws Exception {
if (StrUtil.isBlank(status)) {
return false;
}

//转换枚举类
Class<Enum> clazz = (Class<Enum>) beanClass;
/**
* 其实枚举是语法糖
* 是封装好的多个Enum类的实列
* 获取所有枚举实例
*/
Enum[] enumConstants = clazz.getEnumConstants();

//根据方法名获取方法
Method getCode = clazz.getMethod("getCode");
Method getDesc = clazz.getMethod("getDesc");
for (Enum enums : enumConstants) {
//得到枚举实例名
String instance = enums.name();
//执行枚举方法获得枚举实例对应的值
String code = getCode.invoke(enums).toString();
if (code.equals(status)) {
return true;
}
String desc = getDesc.invoke(enums).toString();
System.out.println(StrFormatter.format("实列{}---code:{}desc{}", instance, code, desc));
}
return false;
}

/**
* 验证金额0.00
*
* @param money
* @return
*/
public static boolean isMoney(BigDecimal money) {
if (StrUtil.isEmptyIfStr(money)) {
return false;
}
if (!NumberUtil.isNumber(String.valueOf(money.doubleValue()))) {
return false;
}
if (money.doubleValue() == 0) {
return false;
}
Matcher m = money_pattern.matcher(String.valueOf(money.doubleValue()));
return m.matches();
}


/**
* 验证 日期
*
* @param date
* @param dateFormat
* @return
*/
public static boolean isDateTime(String date, String dateFormat) {
if (StrUtil.isBlank(date)) {
return false;
}
try {
DateUtil.parse(date, dateFormat);
return true;
} catch (Exception e) {
return false;
}
}

}

我自定义了补充了很多验证器,包括日期验证,枚举验证,手机号验证,金额验证

自定义校验注解使用起来和内置注解无异,在需要的字段上添加相应注解即可

校验流程解析

使用 Validation API 进行参数效验步骤整个过程如下图所示,用户访问接口,然后进行参数效验 ,如果效验通过,则进入业务逻辑,否则抛出异常,交由全局异常处理器进行处理

全局异常出来请参考我这篇文章SpringBoot优雅的全局异常处理

关注公众号猿人生获取更多干货分享

发送参数校验 获取完整自定义源码

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%