好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

SpringBoot Validation快速实现数据校验的示例代码

前言

在实际开发中,肯定会经常遇到对参数字段进行校验的场景,虽然大多数情况下前端都会进行校验,但我们知道前端并不可信,所以后台也需要进行校验,通常我们只能写大量的 if else 来完成校验工作,而如果使用 SpringBoot Validation 则可以轻松的通过注解来完成。

环境配置

引入Jar包

?

1

2

3

4

5

6

7

8

9

< dependency >

     < groupId >org.springframework.boot</ groupId >

     < artifactId >spring-boot-starter-web</ artifactId >

</ dependency >

 

< dependency >

     < groupId >org.springframework.boot</ groupId >

     < artifactId >spring-boot-starter-validation</ artifactId >

</ dependency >

使用示例

?

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

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

@Data

@ToString

public class DemoEntity {

    

     // 不能为空,比较时会除去空格

     @NotBlank (message = "名称不能为空" )

     private String name;

    

     // amount必须是一个大于等于5,小于等于10的数字

     @DecimalMax (value = "10" )

     @DecimalMin (value = "5" )

     private BigDecimal amount;

 

     // 必须符合email格式

     @Email

     private String email;

    

     // size长度必须在5到10之间

     @Size (max = 10 , min = 5 )

     private String size;

    

     // age大小必须在18到35之间

     @Min (value = 18 )

     @Max (value = 35 )

     private int age;

    

     // user不能为null

     @NotNull

     private User user;

    

     // 限制必须为小数,且整数位integer最多2位,小数位fraction最多为4位

     @Digits (integer = 2 , fraction = 4 )

     private BigDecimal digits;

    

     // 限制必须为未来的日期

     @Future

     private Date future;

 

     // 限制必须为过期的日期

     @Past

     private Date past;

    

     // 限制必须是一个未来或现在的时间

     @FutureOrPresent

     private Date futureOrPast;

    

     // 支持正则表达式

     @Pattern (regexp = "^\\d+$" )

     private String digit;

}

注意:请求时,参数必须加上 @Validated 才能生效

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

@RestController

@Slf4j

@RequestMapping ( "/valid" )

public class TestValidController {

 

     @RequestMapping ( "/demo1" )

     public String demo12( @Validated @RequestBody DemoEntity demoEntity) {

         try {

             return "SUCCESS" ;

         } catch (Exception e) {

             log.error(e.getMessage(), e);

             return "FAIL" ;

         }

     }

}

分组

有些时候,同一个参数在不能场景下校验的规则可能不一样,这时候我们就可以通过分组的方式来实现

实体类 name 属性设置了两种校验,分别针对 groups 为A和B的生效

?

1

2

3

@NotBlank (message = "名称不能为空" , groups = A. class )

@Size (max = 10 , min = 5 , groups = B. class )

private String name;

只要在相对应的接口上选择A或者B即可

?

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

@RestController

@Slf4j

@RequestMapping ( "/valid" )

public class TestValidController {

 

     @RequestMapping ( "/demo1" )

     public String demo1( @Validated ({A. class }) @RequestBody DemoEntity demoEntity) {

         try {

             return "SUCCESS" ;

         } catch (Exception e) {

             log.error(e.getMessage(), e);

             return "FAIL" ;

         }

     }

 

     @RequestMapping ( "/demo2" )

     public String demo2( @Validated ({B. class }) @RequestBody DemoEntity demoEntity) {

         try {

             return "SUCCESS" ;

         } catch (Exception e) {

             log.error(e.getMessage(), e);

             return "FAIL" ;

         }

     }

}

到此这篇关于利用SpringBoot Validation快速实现数据校验的文章就介绍到这了,更多相关SpringBoot Validation数据校验内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/CSDN_WYL2016/article/details/125129492

查看更多关于SpringBoot Validation快速实现数据校验的示例代码的详细内容...

  阅读:14次