Hibernate Validation 约束注解(校验注解)

需要掌握:JSR303、JSR349、JSR380、Hibernate Validation、Spring Validation

说明

JSR 提供注解,Hibernate Validation 通过对注解的解析,实现校验

使用例子

https://zhuanlan.zhihu.com/p/444925714

JSR提供的校验注解

空与非空检查

注解 支持Java类型 说明
@Null Object 必须为null
@NotNull Object 必须不为null
@NotBlank CharSequence 不为null,且必须有一个非空格字符
@NotEmpty CharSequence、Collection、Map、Array 不为null,且不为空(length/size>0)

有无@NotBlank的区别

看如下两段代码:

代码一:

@Length(min = 6,max = 20,message = "密码长度在6-20之间")
private String password;

代码二:

@NotBlank(message = "密码不能为空")
@Length(min = 6,max = 20,message = "密码长度在6-20之间")
private String password;

这两段代码的区别就在于方法上是否有@NotBlank的注解。

结果也是有区别的,当访问如下请求的话,

/userInfo/saveUserInfo?username=wuqian&email=aa@qq.com

代码一是可以正常访问的,代码二是就会提示:密码不能为空。

也就是代码一:入参password是可以不填写的,此时就会进行长度的校验了,可以正常的访问。

代码二:入参是必须要填写的,填写的规则必须满足长度要求。

Boolean值检查

注解 支持Java类型 说明 备注
@AssertTrue boolean、Boolean 为true 为null有效
@AssertFalse boolean、Boolean 为false 为null有效

日期检查

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

数值检查

注解 支持Java类型 说明 备注
@Max BigDecimal、BigInteger,byte、short、int、long以及包装类 小于或等于 为null有效
@Min BigDecimal、BigInteger,byte、short、int、long以及包装类 大于或等于 为null有效
@DecimalMax BigDecimal、BigInteger、CharSequence,byte、short、int、long以及包装类 小于或等于 为null有效
@DecimalMin BigDecimal、BigInteger、CharSequence,byte、short、int、long以及包装类 大于或等于 为null有效
@Negative BigDecimal、BigInteger,byte、short、int、long、float、double以及包装类 负数 为null有效,0无效
@NegativeOrZero BigDecimal、BigInteger,byte、short、int、long、float、double以及包装类 负数或零 为null有效
@Positive BigDecimal、BigInteger,byte、short、int、long、float、double以及包装类 正数 为null有效,0无效
@PositiveOrZero BigDecimal、BigInteger,byte、short、int、long、float、double以及包装类 正数或零 为null有效
@Digits(integer = 3, fraction = 2) BigDecimal、BigInteger、CharSequence,byte、short、int、long以及包装类 整数位数和小数位数上限 为null有效

其他

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

hibernate-validator扩展约束(部分)

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

使用例子

姓名长度必须在1-20之间

@NotNull(message = "姓名不能为空")
@Size(min = 1, max = 20, message = "姓名长度必须在1-20之间")
private String name;

年龄需要在10-150之间

@Min(value = 10, message = "年龄必须大于10")
@Max(value = 150, message = "年龄必须小于150")
private Integer age;

邮箱格式校验

@NotBlank(message = "邮箱不能为空")
@Email(message = "邮箱格式不合理")
private String email;

自定义邮箱正则

@Email(regexp = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$",message = "邮箱格式不合理")
private String email;

手机号格式校验

@Pattern(regexp = "^1(3|4|5|7|8)\\d{9}$",message = "手机号码格式错误")
@NotBlank(message = "手机号码不能为空")
private String phone;

参考:
https://zhuanlan.zhihu.com/p/444925714
https://www.jianshu.com/p/3267689ebf1b


原文出处:/show_1IX74zXh1hpy.html