当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot整合表单验证注解@Validated,以及分组验证

SpringBoot整合表单验证注解@Validated,以及分组验证

2019年12月24日  | 移动技术网IT编程  | 我要评论

仔仔网,五四青年节祝福,笔记本wifi无线上网

首先引入jar包

<dependency>
    <groupid>org.hibernate</groupid>
    <artifactid>hibernate-validator</artifactid>
    <version>6.0.7.final</version>
</dependency>

表单注解验证一般可以用来 验证用户名不能为空,邮箱格式不正确,手机号应为11位 ,但是在验证一部分字段的时候要用到分组的功能

创建一个包 名字为group包,创建两个接口类 

 

 

 类里不用写内容

 

添加字段的注解验证

@tablename("user")
@groupsequence({insert.class,update.class,user.class})
public class user extends model<user> implements serializable {
 
    private static final long serialversionuid=1l;
 
    @tableid(value = "id", type = idtype.auto)
    private long id;
 
    @notempty(groups={insert.class,update.class},message = "用户名不能为空")
    @size(groups={insert.class,update.class},min =  5,max = 16 ,message = "用户名要求5-16位")
    private string uname;
 
    @notempty(groups={insert.class},message = "密码不能为空")
    private string upwd;
 
    @notempty(groups={insert.class,update.class},message = "邮箱不能为空")
    @email(groups={insert.class,update.class},message = "邮箱格式不对")
    private string uemail;
 
    @notempty(groups={insert.class,update.class},message = "手机号不能为空")
     @size(groups={insert.class,update.class},min =  11,max = 11 ,message = "手机号为11位有效手机号")
    private string utel;
     
    @notempty(groups={insert.class,update.class},message = "角色不能为空")
    private string urank;

验证前端传来的数据

@postmapping(value = "/registerinfo")
    @responsebody
    public msg register(@requestbody @validated({ insert.class }) user user, bindingresult bindingresult) {
                //获取验证注解信息
        if (bindingresult.haserrors()) {
            return msg.failure(bindingresult.getfielderror().getdefaultmessage());
        } else {
 
            return "验证正确";
        }
 
    }

例如以上为user表的注解验证

 

@notempty  不能为空    @size  设置长度   @emaile  设置邮箱格式   更多的注解可以页面最后查询
@groupsequence({insert.class,update.class,user.class})     //引入两个接口类
@notempty(groups={insert.class},message = "密码不能为空")   //设置在 @validated({ insert.class }) 的时候只验证密码,   如果@validated({ update.class })的时候,密码就不验证了
@notempty(groups={insert.class,update.class},message = "用户名不能为空")   //在设置 @validated({ insert.class })或者 @validated({ update.class })的时候可以验证用户名

 

验证注解

验证的数据类型

说明

@assertfalse

boolean,boolean

验证注解的元素值是false

@asserttrue

boolean,boolean

验证注解的元素值是true

@notnull

任意类型

验证注解的元素值不是null

@null

任意类型

验证注解的元素值是null

@min(value=值)

bigdecimal,biginteger, byte,

short, int, long,等任何number或charsequence(存储的是数字)子类型

验证注解的元素值大于等于@min指定的value值

@max(value=值)

和@min要求一样

验证注解的元素值小于等于@max指定的value值

@decimalmin(value=值)

和@min要求一样

验证注解的元素值大于等于@ decimalmin指定的value值

@decimalmax(value=值)

和@min要求一样

验证注解的元素值小于等于@ decimalmax指定的value值

@digits(integer=整数位数, fraction=小数位数)

和@min要求一样

验证注解的元素值的整数位数和小数位数上限

@si***=下限, max=上限)

字符串、collection、map、数组等

验证注解的元素值的在min和max(包含)指定区间之内,如字符长度、集合大小

@past

java.util.date,

java.util.calendar;

joda time类库的日期类型

验证注解的元素值(日期类型)比当前时间早

@future

与@past要求一样

验证注解的元素值(日期类型)比当前时间晚

@notblank

charsequence子类型

验证注解的元素值不为空(不为null、去除首位空格后长度为0),不同于@notempty,@notblank只应用于字符串且在比较时会去除字符串的首位空格

@length(min=下限, max=上限)

charsequence子类型

验证注解的元素值长度在min和max区间内

@notempty

charsequence子类型、collection、map、数组

验证注解的元素值不为null且不为空(字符串长度不为0、集合大小不为0)

@range(min=最小值, max=最大值)

bigdecimal,biginteger,charsequence, byte, short, int, long等原子类型和包装类型

验证注解的元素值在最小值和最大值之间

@email(regexp=正则表达式,

flag=标志的模式)

charsequence子类型(如string)

验证注解的元素值是email,也可以通过regexp和flag指定自定义的email格式

@pattern(regexp=正则表达式,

flag=标志的模式)

string,任何charsequence的子类型

验证注解的元素值与指定的正则表达式匹配

@valid

任何非原子类型

指定递归验证关联的对象;

如用户对象中有个地址对象属性,如果想在验证用户对象时一起验证地址对象的话,在地址对象上加@valid注解即可级联验证

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网