怎么在Springboot中對字段進行校驗-創(chuàng)新互聯(lián)

怎么在Springboot中對字段進行校驗?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

創(chuàng)新互聯(lián)公司堅持“要么做到,要么別承諾”的工作理念,服務領域包括:做網站、網站制作、企業(yè)官網、英文網站、手機端網站、網站推廣等服務,滿足客戶于互聯(lián)網時代的新城網站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯(lián)網解決方案。努力成為您成熟可靠的網絡建設合作伙伴!

Controller層 VS Service層


去網上查閱了一些資料,一般推薦與業(yè)務無關的放在Controller層中進行校驗,而與業(yè)務有關的放在Service層中進行校驗。那么如何將參數校驗寫的優(yōu)雅美觀呢,如果都是if - else,就感覺代碼寫的很low,還好有輪子可以使用

常用校驗工具類

使用Hibernate Validate

引入依賴

<dependency>
 <groupId>org.hibernate</groupId>
 <artifactId>hibernate-validator</artifactId>
 <version>4.3.1.Final</version> 
</dependency>

常用注解說明

注解說明
@Length(min=,max=)檢查所屬的字段的長度是否在min和max之間,只能用于字符串
@Range(min=,max=,message=)被注釋的元素必須在合適的范圍內
@Max該字段的值只能小于或等于該值
@Min該字段的值只能大于或等于該值
@NotNull不能為null
@NotBlank不能為空,檢查時會將空格忽略
@NotEmpty不能為空,這里的空是指空字符串
@Pattern(regex=,flag=)被注釋的元素必須符合指定的正則表達式

使用姿勢

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

注解@Validated@Valid
所屬的包屬于org.springframework.validation.annotation包下的,是spring提供的屬于javax.validation包下,是jdk給提供的
是否支持分組和排序

雖然@Validated比@Valid更加強大,在@Valid之上提供了分組功能和驗證排序功能,不過在實際項目中一直沒有用到過
Hibernate-validate框架中的注解是需要加在實體中一起使用的

定義一個實體

public class DataSetSaveVO {
 //標識符為空
 @NotBlank(message = "user uuid is empty")
 //用戶名稱只能是字母和數字
 @Pattern(regexp = "^[a-z0-9]+$", message = "user names can only be alphabetic and numeric")
 @Length(max = 48, message = "user uuid length over 48 byte")
 private String userUuid;

 //數據集名稱只能是字母和數字
 @Pattern(regexp = "^[A-Za-z0-9]+$", message = "data set names can only be letters and Numbers")
 //文件名稱過長
 @Length(max = 48, message = "file name too long")
 //文件名稱為空
 @NotBlank(message = "file name is empty")
 private String name;

 //數據集描述最多為256字節(jié)
 @Length(max = 256, message = "data set description length over 256 byte")
 //數據集描述為空
 @NotBlank(message = "data set description is null")
 private String description;
}

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

Controller層中的方法

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

說明:在校驗的實體DataSetSaveVO旁邊添加@Valid或@Validated注解

使用commons-lang3

引入依賴

<dependency>
 <groupId>org.apache.commons</groupId>
 <artifactId>commons-lang3</artifactId>
 <version>3.4</version>
</dependency>

常用方法說明

方法說明
CollectionUtils.isEmpty判斷集合是否為空,為null或者size==0,返回true
CollectionUtils.isNotEmpty判斷集合是否為非空
StringUtils.isEmpty判斷字符串是否為空
StringUtils.isNotEmpty判斷字符串是否非空
StringUtils.isBlank判斷字符串是否為空,為null或者size==0或者只存在空白字符(如" "),則返回true
StringUtils.isNotBlank判斷字符串是否為非空

測試代碼

//StringUtils.isEmpty
System.out.println(StringUtils.isEmpty("")); //true
System.out.println(StringUtils.isEmpty(" ")); //false
//StringUtils.isNotEmpty
System.out.println(StringUtils.isNotEmpty("")); //false
 
//StringUtils.isBlank
System.out.println(StringUtils.isBlank("")); //true
System.out.println(StringUtils.isBlank(" ")); //true
//StringUtils.isNotBlank
System.out.println(StringUtils.isNotBlank(" ")); //false

List<Integer> emptyList = new ArrayList<>();
List<Integer> nullList = null;
List<Integer> notEmptyList = new ArrayList<>();
notEmptyList.add(1);

//CollectionUtils.isEmpty
System.out.println(CollectionUtils.isEmpty(emptyList)); //true
System.out.println(CollectionUtils.isEmpty(nullList)); //true
System.out.println(CollectionUtils.isEmpty(notEmptyList)); //false

//CollectionUtils.isNotEmpty
System.out.println(CollectionUtils.isNotEmpty(emptyList)); //false
System.out.println(CollectionUtils.isNotEmpty(nullList)); //false
System.out.println(CollectionUtils.isNotEmpty(notEmptyList)); //true

看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)網站建設公司,的支持。

文章標題:怎么在Springboot中對字段進行校驗-創(chuàng)新互聯(lián)
本文網址:http://muchs.cn/article32/cshdsc.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供動態(tài)網站、網站建設、域名注冊、品牌網站設計、軟件開發(fā)、網站排名

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網站建設