Springboot中优雅进行字段校验

开发 后端
前段时间提交代码审核,同事提了一个代码规范缺陷:参数校验应该放在controller层。到底应该如何做参数校验呢?来看一下吧。

 [[434294]]

前段时间提交代码审核,同事提了一个代码规范缺陷:参数校验应该放在controller层。到底应该如何做参数校验呢?

Controller层 VS Service层

去网上查阅了一些资料,一般推荐与业务无关的放在Controller层中进行校验,而与业务有关的放在Service层中进行校验。

那么如何将参数校验写的优雅美观呢,如果都是if - else,就感觉代码写的很low,还好有轮子可以使用。

常用校验工具类

使用Hibernate Validate

引入依赖 

  1. <dependency>  
  2.     <groupId>org.hibernate</groupId>  
  3.     <artifactId>hibernate-validator</artifactId>  
  4.     <version>4.3.1.Final</version>   
  5. </dependency> 

常用注解说明

使用姿势

需要搭配在Controller中搭配@Validated或@Valid注解一起使用,@Validated和@Valid注解区别不是很大,一般情况下任选一个即可,区别如下:

虽然@Validated比@Valid更加强大,在@Valid之上提供了分组功能和验证排序功能,不过在实际项目中一直没有用到过。

Hibernate-validate框架中的注解是需要加在实体中一起使用的。

~ 定义一个实体: 

  1. public class DataSetSaveVO {  
  2.     //唯一标识符为空  
  3.     @NotBlank(message = "user uuid is empty" 
  4.     //用户名称只能是字母和数字  
  5.     @Pattern(regexp = "^[a-z0-9]+$"message = "user names can only be alphabetic and numeric" 
  6.     @Length(max = 48message = "user uuid length over 48 byte" 
  7.     private String userUuid;  
  8.     //数据集名称只能是字母和数字  
  9.     @Pattern(regexp = "^[A-Za-z0-9]+$"message = "data set names can only be letters and Numbers" 
  10.     //文件名称过长  
  11.     @Length(max = 48message = "file name too long" 
  12.     //文件名称为空 
  13.     @NotBlank(message = "file name is empty" 
  14.     private String name;  
  15.     //数据集描述最多为256字节  
  16.     @Length(max = 256message = "data set description length over 256 byte" 
  17.     //数据集描述为空  
  18.     @NotBlank(message = "data set description is null" 
  19.     private String description;  

说明:message字段为不符合校验规则时抛出的异常信息。

~ Controller层中的方法: 

  1. @PostMapping  
  2. public ResponseVO createDataSet(@Valid @RequestBody DataSetSaveVO dataSetVO) {  
  3.     return ResponseUtil.success(dataSetService.saveDataSet(dataSetVO));  

说明:在校验的实体DataSetSaveVO旁边添加@Valid或@Validated注解。

使用commons-lang3

引入依赖 

  1. <dependency>  
  2.     <groupId>org.apache.commons</groupId>  
  3.     <artifactId>commons-lang3</artifactId>  
  4.     <version>3.4</version>  
  5. </dependency> 

常用方法说明

测试代码 

  1. //StringUtils.isEmpty  
  2. System.out.println(StringUtils.isEmpty(""));  //true  
  3. System.out.println(StringUtils.isEmpty("  "));  //false  
  4. //StringUtils.isNotEmpty  
  5. System.out.println(StringUtils.isNotEmpty(""));  //false       
  6. //StringUtils.isBlank  
  7. System.out.println(StringUtils.isBlank(""));  //true  
  8. System.out.println(StringUtils.isBlank(" "));  //true  
  9. //StringUtils.isNotBlank  
  10. System.out.println(StringUtils.isNotBlank(" "));  //false  
  11. List<Integer> emptyList = new ArrayList<>();  
  12. List<Integer> nullnullList = null;  
  13. List<Integer> notEmptyList = new ArrayList<>();  
  14. notEmptyList.add(1);  
  15. //CollectionUtils.isEmpty  
  16. System.out.println(CollectionUtils.isEmpty(emptyList));   //true  
  17. System.out.println(CollectionUtils.isEmpty(nullList));   //true  
  18. System.out.println(CollectionUtils.isEmpty(notEmptyList));   //false  
  19. //CollectionUtils.isNotEmpty  
  20. System.out.println(CollectionUtils.isNotEmpty(emptyList));   //false  
  21. System.out.println(CollectionUtils.isNotEmpty(nullList));   //false  
  22. System.out.println(CollectionUtils.isNotEmpty(notEmptyList));   //true 

自定义注解

当上面的方面都无法满足校验的需求以后,可以考虑使用自定义注解。

责任编辑:庞桂玉 来源: Hollis
相关推荐

2021-10-22 14:50:23

Spring BootJava

2023-03-16 08:23:33

2023-11-29 07:23:04

参数springboto

2023-03-28 08:07:12

2019-01-24 16:11:19

前端全局异常数据校验

2022-05-03 10:43:43

SpringJava

2022-08-03 07:07:10

Spring数据封装框架

2023-03-06 11:36:13

SpingBoot注解

2020-12-08 08:08:51

Java接口数据

2023-12-20 13:50:00

SpringBootJSON序列化

2023-03-23 22:46:38

Spring限流机制

2023-06-28 08:25:14

事务SQL语句

2023-01-30 07:41:43

2022-12-30 08:49:41

SpringBoot@Validated

2020-08-29 19:28:08

版本回退命令代码

2023-11-22 13:05:12

Pytest测试

2019-01-21 09:28:32

版本命令程序员

2024-03-18 14:06:00

停机Spring服务器

2020-03-25 17:55:30

SpringBoot拦截器Java

2020-01-10 16:23:44

Springboot停止服务Java
点赞
收藏

51CTO技术栈公众号