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

Spring Boot進行優(yōu)雅的字段校驗,寫得太好了!

開發(fā) 后端
前段時間提交代碼審核,同事提了一個代碼規(guī)范缺陷:參數(shù)校驗應該放在controller層。到底應該如何做參數(shù)校驗呢?

前段時間提交代碼審核,同事提了一個代碼規(guī)范缺陷:參數(shù)校驗應該放在controller層。到底應該如何做參數(shù)校驗呢? 

Controller層 VS Service層

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

那么如何將參數(shù)校驗寫的優(yōu)雅美觀呢,如果都是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> 

常用注解說明 

 

使用姿勢

Spring Boot 基礎就不介紹了,推薦下這個實戰(zhàn)教程:

https://github.com/javastacks/spring-boot-best-practice

需要搭配在Controller中搭配@Validated或@Valid注解一起使用,@Validated和@Valid注解區(qū)別不是很大,一般情況下任選一個即可,區(qū)別如下:

 

雖然@Validated比@Valid更加強大,在@Valid之上提供了分組功能和驗證排序功能,不過在實際項目中一直沒有用到過

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

定義一個實體 

  1. public class DataSetSaveVO { 
  2.     //唯一標識符為空 
  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 = 48, message = "user uuid length over 48 byte"
  7.     private String userUuid; 
  8.  
  9.     //數(shù)據(jù)集名稱只能是字母和數(shù)字 
  10.     @Pattern(regexp = "^[A-Za-z0-9]+$", message = "data set names can only be letters and Numbers"
  11.     //文件名稱過長 
  12.     @Length(max = 48, message = "file name too long"
  13.     //文件名稱為空 
  14.     @NotBlank(message = "file name is empty"
  15.     private String name
  16.  
  17.     //數(shù)據(jù)集描述最多為256字節(jié) 
  18.     @Length(max = 256, message = "data set description length over 256 byte"
  19.     //數(shù)據(jù)集描述為空 
  20.     @NotBlank(message = "data set description is null"
  21.     private String description; 

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

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.          
  7. //StringUtils.isBlank 
  8. System.out.println(StringUtils.isBlank(""));  //true 
  9. System.out.println(StringUtils.isBlank(" "));  //true 
  10. //StringUtils.isNotBlank 
  11. System.out.println(StringUtils.isNotBlank(" "));  //false 
  12.  
  13. List<Integer> emptyList = new ArrayList<>(); 
  14. List<Integer> nullList = null
  15. List<Integer> notEmptyList = new ArrayList<>(); 
  16. notEmptyList.add(1); 
  17.  
  18. //CollectionUtils.isEmpty 
  19. System.out.println(CollectionUtils.isEmpty(emptyList));   //true 
  20. System.out.println(CollectionUtils.isEmpty(nullList));   //true 
  21. System.out.println(CollectionUtils.isEmpty(notEmptyList));   //false 
  22.  
  23. //CollectionUtils.isNotEmpty 
  24. System.out.println(CollectionUtils.isNotEmpty(emptyList));   //false 
  25. System.out.println(CollectionUtils.isNotEmpty(nullList));   //false 
  26. System.out.println(CollectionUtils.isNotEmpty(notEmptyList));   //true 

自定義注解

當上面的方面都無法滿足校驗的需求以后,可以考慮使用自定義注解。

 

責任編輯:龐桂玉 來源: Java技術棧
相關推薦

2020-05-07 10:05:58

Spring循環(huán)依賴Java

2020-07-29 10:40:21

Spring循環(huán)依賴Java

2021-06-25 09:47:59

Spring循環(huán)依賴Java

2018-09-21 15:50:58

數(shù)據(jù)庫MySQL分庫分表

2019-09-29 10:04:26

技術編程開發(fā)

2019-10-17 09:26:05

MySQL數(shù)據(jù)庫InnoDB

2021-06-21 15:57:08

微服務架構數(shù)據(jù)

2020-11-12 07:43:06

Redis冪等性接口

2021-10-27 09:55:55

Sharding-Jd分庫分表Java

2020-06-18 11:58:13

蘋果MacOS保密

2021-11-10 10:03:18

SpringBootJava代碼

2020-09-09 09:55:36

JavaNIOBIO

2022-01-04 09:53:37

Python多線程多進程

2024-12-06 09:27:28

2020-04-10 08:28:44

數(shù)據(jù)裁員行業(yè)

2020-10-18 07:24:16

數(shù)字證書簽名網(wǎng)絡協(xié)議

2021-08-12 10:32:50

Spring Boot參數(shù)校驗分組校驗

2025-04-10 00:22:22

Spring@JsonView字段

2021-08-10 15:11:27

Spring Boot參數(shù)校驗

2024-08-02 09:15:22

Spring捕捉格式
點贊
收藏

51CTO技術棧公眾號