自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Springboot中優(yōu)雅進(jìn)行字段校驗(yàn)

開發(fā) 后端
前段時間提交代碼審核,同事提了一個代碼規(guī)范缺陷:參數(shù)校驗(yàn)應(yīng)該放在controller層。到底應(yīng)該如何做參數(shù)校驗(yàn)?zāi)??來看一下吧?/div>

 [[434294]]

前段時間提交代碼審核,同事提了一個代碼規(guī)范缺陷:參數(shù)校驗(yàn)應(yīng)該放在controller層。到底應(yīng)該如何做參數(shù)校驗(yàn)?zāi)兀?/p>

Controller層 VS Service層

去網(wǎng)上查閱了一些資料,一般推薦與業(yè)務(wù)無關(guān)的放在Controller層中進(jìn)行校驗(yàn),而與業(yè)務(wù)有關(guān)的放在Service層中進(jìn)行校驗(yàn)。

那么如何將參數(shù)校驗(yàn)寫的優(yōu)雅美觀呢,如果都是if - else,就感覺代碼寫的很low,還好有輪子可以使用。

常用校驗(yàn)工具類

使用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注解區(qū)別不是很大,一般情況下任選一個即可,區(qū)別如下:

雖然@Validated比@Valid更加強(qiáng)大,在@Valid之上提供了分組功能和驗(yàn)證排序功能,不過在實(shí)際項(xiàng)目中一直沒有用到過。

Hibernate-validate框架中的注解是需要加在實(shí)體中一起使用的。

~ 定義一個實(shí)體: 

  1. public class DataSetSaveVO {  
  2.     //唯一標(biāo)識符為空  
  3.     @NotBlank(message = "user uuid is empty" 
  4.     //用戶名稱只能是字母和數(shù)字  
  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.     //數(shù)據(jù)集名稱只能是字母和數(shù)字  
  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.     //數(shù)據(jù)集描述最多為256字節(jié)  
  16.     @Length(max = 256message = "data set description length over 256 byte" 
  17.     //數(shù)據(jù)集描述為空  
  18.     @NotBlank(message = "data set description is null" 
  19.     private String description;  

說明:message字段為不符合校驗(yàn)規(guī)則時拋出的異常信息。

~ Controller層中的方法: 

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

說明:在校驗(yàn)的實(shí)體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 

自定義注解

當(dāng)上面的方面都無法滿足校驗(yàn)的需求以后,可以考慮使用自定義注解。

責(zé)任編輯:龐桂玉 來源: Hollis
相關(guān)推薦

2021-10-22 14:50:23

Spring BootJava

2023-03-16 08:23:33

2023-11-29 07:23:04

參數(shù)springboto

2023-03-28 08:07:12

2019-01-24 16:11:19

前端全局異常數(shù)據(jù)校驗(yàn)

2022-05-03 10:43:43

SpringJava

2022-08-03 07:07:10

Spring數(shù)據(jù)封裝框架

2024-06-19 10:04:15

ifC#代碼

2023-03-06 11:36:13

SpingBoot注解

2024-04-01 09:24:39

2020-12-08 08:08:51

Java接口數(shù)據(jù)

2023-12-20 13:50:00

SpringBootJSON序列化

2023-03-23 22:46:38

Spring限流機(jī)制

2024-12-26 07:47:05

Spring管理配置

2023-06-28 08:25:14

事務(wù)SQL語句

2023-01-30 07:41:43

2019-01-21 09:28:32

版本命令程序員

2023-11-22 13:05:12

Pytest測試

2020-08-29 19:28:08

版本回退命令代碼

2025-03-17 00:00:00

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號