服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - SpringBoot 使用hibernate validator校验

SpringBoot 使用hibernate validator校验

2021-06-17 11:33殷天文 Java教程

这篇文章主要介绍了SpringBoot 使用hibernate validator校验,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

本文将全面的介绍如何使用 validator 进行数据校验

本文源码: https://gitee.com/yintianwen7/taven-springboot-learning/tree/master/springboot-validate

准备工作

我们只需要引入 spring-boot-starter-web 包即可使用

1.常用注解

SpringBoot 使用hibernate validator校验

常用注解

2.简单的实体校验

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class carddto {
 
  @notblank
  private string cardid;
 
  @size(min = 10, max = 10)
  @notnull
  private string cardnum; // 卡号
 
  @past
  @notnull
  private date createdate;
 
  @range(max = 3)
  private string cardtype;
 
  // 省略get set
}
?
1
2
3
4
5
6
7
8
9
@restcontroller
public class usercontroller {
 
  @postmapping("simple")
  public object simple(@requestbody @valid carddto carddto) {
    return carddto;
  }
 
}
  • 实体属性上添加校验注解
  • controller 方法 参数前 使用@valid 即可

3. 复杂的实体校验

 3.1 嵌套实体校验

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class userdto {
 
  @notblank
  private string userid;
 
  @notblank
  private string username;
 
  private string password;
 
  @valid
  private list<carddto> cardlist;
 
  //省略 get set
}

controller 写法 同上,只是在 userdto cardlist 属性上标记@valid 注解 即可。 3.2 list<dto> 校验

SpringBoot 使用hibernate validator校验

无效示例

如果我们想校验 一个实体list,如上图所示的这种写法是完全不起效的。

我们需要像 嵌套校验 时一样,对 list<carddto> 做一层封装

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class validlist<e> implements list<e> {
 
  @valid
  private list<e> list = new arraylist<>();
 
  public list<e> getlist() {
    return list;
  }
 
  public void setlist(list<e> list) {
    this.list = list;
  }
 
  // 省略了 实现方法
}

重写实现方法完全使用 this.list.xxx()

gitee:spring 会将数据封装到我们定义的 list 属性中,又将属性声明了 @valid 使得 hibernate validator 可以为我们做校验!

3.3 使用 @validated 分组校验

?
1
2
3
4
5
public interface insert {
}
 
public interface update {
}

定义两个接口

?
1
2
3
4
5
6
7
8
9
10
11
12
13
public class groupcarddto {
 
  @notblank(groups = {update.class})
  private string id;
 
  @notblank(groups = {insert.class})
  private string cardnum;
 
  @notnull(groups = {insert.class, update.class})
  private integer cardtype;
 
  //省略 get set
}

实体标记的注解中添加 group 属性

?
1
2
3
4
@postmapping("insert_card")
  public object insert_card(@requestbody @validated(insert.class) groupcarddto card){
    return card;
  }

使用 @validated(xxx.class) 标记参数,完成分组校验!

4.自定义注解校验

当 validator 提供的注解无法满足我们的业务需求,可以通过自定义的方式来实现校验。

需求:校验某字符串必须为大写或者小写

?
1
2
3
4
public enum casemode {
  upper,
  lower
}

定义一个枚举类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import javax.validation.constraint;
import javax.validation.payload;
import java.lang.annotation.*;
 
@target( { elementtype.field })
@retention(retentionpolicy.runtime)
@constraint(validatedby = checkcasevalidator.class)
@documented
public @interface checkcase {
 
  string message() default "";
 
  class<?>[] groups() default {};
 
  class<? extends payload>[] payload() default {};
 
  casemode value() default casemode.lower;
 
}
  • 定义注解
  • @constraint 指定我们的校验逻辑实现类
?
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
import javax.validation.constraintvalidator;
import javax.validation.constraintvalidatorcontext;
 
public class checkcasevalidator implements constraintvalidator<checkcase, string> {
 
  private casemode casemode;
 
  @override
  public void initialize(checkcase constraintannotation) {
    this.casemode = constraintannotation.value();
  }
 
  @override
  public boolean isvalid(string value, constraintvalidatorcontext context) {
 
    if (value == null || "".equals(value.trim())) {
      return false;
    }
 
    switch (this.casemode) {
      case lower:
        return value.equals(value.tolowercase());
      case upper:
        return value.equals(value.touppercase());
      default:
        return false;
    }
 
  }
 
}
  • initialize() 初始化时执行,可以用来获取注解中的属性
  • isvalid() 实现我们的校验逻辑

备注

我们自定义的注解依然支持 @validated group 分组

本节源码: https://gitee.com/yintianwen7/taven-springboot-learning/tree/master/springboot-validate

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.jianshu.com/p/39279c025b15

延伸 · 阅读

精彩推荐