Springboot:validator

149 阅读2分钟

常用注解

空与非空检查

注解支持 Java 类型说明
@NullObject为 null
@NotNullObject不为 null
@NotBlankCharSequence不为 null,且必须有一个非空格字符
@NotEmptyCharSequence、Collection、Map、Array不为 null,且不为空(length/size>0)

Boolean 值检查

注解支持 Java 类型说明备注
@AssertTrueboolean、Boolean为 true为 null 有效
@AssertFalseboolean、Boolean为 false为 null 有效

日期检查

注解支持 Java 类型说明备注
@FutureDate、Calendar、Instant、LocalDate、LocalDateTime、LocalTime、MonthDay、OffsetDateTime、OffsetTime、Year、YearMonth、ZonedDateTime、HijrahDate、JapaneseDate、MinguoDate、ThaiBuddhistDate验证日期为当前时间之后为 null 有效
@FutureOrPresentDate、Calendar、Instant、LocalDate、LocalDateTime、LocalTime、MonthDay、OffsetDateTime、OffsetTime、Year、YearMonth、ZonedDateTime、HijrahDate、JapaneseDate、MinguoDate、ThaiBuddhistDate验证日期为当前时间或之后为 null 有效
@PastDate、Calendar、Instant、LocalDate、LocalDateTime、LocalTime、MonthDay、OffsetDateTime、OffsetTime、Year、YearMonth、ZonedDateTime、HijrahDate、JapaneseDate、MinguoDate、ThaiBuddhistDate验证日期为当前时间之前为 null 有效
@PastOrPresentDate、Calendar、Instant、LocalDate、LocalDateTime、LocalTime、MonthDay、OffsetDateTime、OffsetTime、Year、YearMonth、ZonedDateTime、HijrahDate、JapaneseDate、MinguoDate、ThaiBuddhistDate验证日期为当前时间或之前为 null 有效

数值检查

注解支持 Java 类型说明备注
@MaxBigDecimal、BigInteger,byte、short、int、long 以及包装类小于或等于为 null 有效
@MinBigDecimal、BigInteger,byte、short、int、long 以及包装类大于或等于为 null 有效
@DecimalMaxBigDecimal、BigInteger、CharSequence,byte、short、int、long 以及包装类小于或等于为 null 有效
@DecimalMinBigDecimal、BigInteger、CharSequence,byte、short、int、long 以及包装类大于或等于为 null 有效
@NegativeBigDecimal、BigInteger,byte、short、int、long、float、double 以及包装类负数为 null 有效,0 无效
@NegativeOrZeroBigDecimal、BigInteger,byte、short、int、long、float、double 以及包装类负数或零为 null 有效
@PositiveBigDecimal、BigInteger,byte、short、int、long、float、double 以及包装类正数为 null 有效,0 无效
@PositiveOrZeroBigDecimal、BigInteger,byte、short、int、long、float、double 以及包装类正数或零为 null 有效
@Digits(integer = 3, fraction = 2)BigDecimal、BigInteger、CharSequence,byte、short、int、long 以及包装类整数位数和小数位数上限为 null 有效

其他

注解支持 Java 类型说明备注
@PatternCharSequence匹配指定的正则表达式为 null 有效
@EmailCharSequence邮箱地址为 null 有效,默认正则 `'.*'`
@SizeCharSequence、Collection、Map、Array大小范围(length/size>0)为 null 有效

hibernate-validator 扩展约束(部分)

注解支持 Java 类型说明
@LengthString字符串长度范围
@Range数值类型和 String指定范围
@URLURL 地址验证

参考:

springboot 2.x 校验www.jianshu.com/p/5949ebcf2…

hibernate校验最佳实践 www.cnblogs.com/vcmq/p/1254…

自定义校验blog.csdn.net/calm_encode…

springmvc校验 blog.csdn.net/xu191665942…

参数校验进阶:blog.csdn.net/jinjiankang…

struts2 校验:blog.csdn.net/SuperstarSt…

docs.spring.io/spring-boot…

优雅校验:zhuanlan.zhihu.com/p/97555913