导图社区 Validation
针对Spring中的validation参数校验,做了详细的记录,包括:常用注解、使用方式、配置、优秀博客、国际化。
编辑于2022-03-15 12:50:57validation
常用注解
启动
@Valid
来源
Hibernate validation
注解位置
构造函数
方法
方法参数
成员属性
标识只要用到这个Bean就会自动校验
注意
不支持类
嵌套验证
支持
实例
1
public class Item implements Serializable { @NotNull(message = "id不能为空") @Min(value = 1, message = "id必须为正整数") private Long id; @Valid @NotNull(message = "users 不能为空") @Size(min = 1, message = "users 至少要有一个属性") private List<User> users; }
分组
不支持
使用场景
多对象
每个对象单独添加
@Validated
Spring Validator
注解位置
类
在类级别上标注@Validated注解告诉Spring需要校验方法参数上的约束
Controller/Service均可
方法
方法参数
不能用于成员属性
嵌套验证
不支持
分组
支持
校验
日期检查
@Past
验证 Date 和 Calendar 对象是否在当前时间之前
@Future
验证 Date 和 Calendar 对象是否在当前时间之后
@FutureOrPresent
验证日期为当前时间或之后
@PastOrPresent
验证日期为当前时间或之前
支持类型
Date、Calendar、Instant、LocalDate、LocalDateTime、LocalTime、MonthDay、OffsetDateTime、OffsetTime、Year、YearMonth、ZonedDateTime、HijrahDate、JapaneseDate、MinguoDate、ThaiBuddhistDate
正则
@Pattern
验证 String 对象是否符合正则表达式的规则
支持
CharSequence
空检查
@NotNull
验证对象是否不为null, 无法查检长度为0的字符串
@NotBlank
检查约束字符串是不是Null还有被Trim的长度是否大于0,只对字符串,且会去掉前后空格
@Null
验证对象是否为null
@NotEmpt
检查约束元素是否为NULL或者是EMPTY
用在集合类上面
支持
CharSequence、Collection、Map、Array
Booelan检查
@AssertTrue
验证 Boolean 对象是否为 true
@AssertFalse
验证 Boolean 对象是否为 false
支持
boolean、Boolean
长度检查
@Size(min=, max=)
验证对象(Array,Collection,Map,String)长度是否在给定的范围之内
支持
CharSequence、Collection、Map、Array
@Length(min=, max=)
被注释的字符串的大小必须在指定的范围内
支持
String
数值检查
建议
使用在Stirng,Integer类型,不建议使用在int类型上,因为表单值为“”时无法转换为int,但可以转换为Stirng为"",Integer为null
@Min
验证 Number 和 String 对象是否大等于指定的值
支持
BigDecimal、BigInteger,byte、short、int、long以及包装类
@Max
验证 Number 和 String 对象是否小等于指定的值
支持
BigDecimal、BigInteger,byte、short、int、long以及包装类
@Range(min=, max=)
@DecimalMax
被标注的值必须不大于约束中指定的最大值. 这个约束的参数是一个通过BigDecimal定义的最大值的字符串表示.小数存在精度
支持
BigDecimal、BigInteger、CharSequence,byte、short、int、long以及包装类
@DecimalMin
被标注的值必须不小于约束中指定的最小值. 这个约束的参数是一个通过BigDecimal定义的最小值的字符串表示.小数存在精度
支持
BigDecimal、BigInteger、CharSequence,byte、short、int、long以及包装类
@Digits(integer=,fraction=)
验证 Number 和 String 的构成是否是合法数字
验证字符串是否是符合指定格式的数字,interger指定整数精度,fraction指定小数精度
支持
BigDecimal、BigInteger、CharSequence,byte、short、int、long以及包装类
@Negative
负数
支持
BigDecimal、BigInteger,byte、short、int、long、float、double以及包装类
@NegativeOrZero
负数或零
支持
BigDecimal、BigInteger,byte、short、int、long、float、double以及包装类
@Positive
正数
支持
BigDecimal、BigInteger,byte、short、int、long、float、double以及包装类
@PositiveOrZero
正数或零
支持
BigDecimal、BigInteger,byte、short、int、long、float、double以及包装类
其他
@CreditCardNumber
信用卡验证
邮箱
@ScriptAssert(lang= ,script=, alias=)
@URL(protocol=,host=, port=,regexp=, flags=)
自定义
创建约束注解
实现一个验证器
定义默认的错误信息
概要
1
@Documented @Target({ElementType.PARAMETER, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Constraint(validatedBy = IdentityCardNumberValidator.class) public @interface IdentityCardNumber { String message() default "身份证号码不合法"; Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; }
使用方式
Controller层
入参Bean
Bean前添加@Valid或@Validated
触发异常
MethodArgumentNotValidException
独立入参
类上加@Validated
入参前加限制注解
概要
触发异常
ConstraintViolationException
Service层
校验入参Bean
类上加@Validated
入参Bean前添加@Valid
独立入参
类上加@Validated
入参前加限制注解
手动校验
Validator.validate()
//实例化一个 validator工厂 ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory(); //获取validator实例 Validator validator = validatorFactory.getValidator(); //调用调用,得到校验结果信息 Set Set<ConstraintViolation<Employee>> constraintViolationSet = validator.validate(employee);
配置
Hibernate
校验模式
普通模式
快速失败
pom引入
<2.3.x
存在spring-boot-starter-web时无需手动引入
>2.3.x
必须手动引入
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency>
优秀博客
https://www.cnblogs.com/mr-yang-localhost/p/7812038.html
https://glory.blog.csdn.net/article/details/119850168
国际化