淺談Spring中@NotEmpty、@NotBlank、@NotNull區(qū)別
1:引入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.0.5.RELEASE</version> </dependency>
@NotEmpty、@NotBlank、@NotNull 包的位置:
import javax.validation.constraints.*;
2:區(qū)別
@NotNull
適用于基本數據類型(Integer,Long,Double等等),當 @NotNull 注解被使用在 String 類型的數據上,則表示該數據不能為 Null(但是可以為 Empty)
注:被其標注的字段可以使用 @size、@Max、@Min 對字段數值進行大小的控制
@NotBlank
適用于 String 類型的數據上,加了@NotBlank 注解的參數不能為 Null 且 trim() 之后 size > 0,必須有實際字符
@NotEmpty
適用于 String、Collection集合、Map、數組等等,加了@NotEmpty 注解的參數不能為 Null 或者 長度為 0
3:使用方法
@Data public class BigPeople { @ApiModelProperty(value = "id" ,required = true) @NotNull(message = "id不能為空") @Length(message = "id不能超過{max}個長度",max = 10) private Integer id; @ApiModelProperty(value = "name" ,required = true) @NotBlank(message = "name不能為空") @Size(message = "名字最長為{max} 個字",max = 10) private String name; @ApiModelProperty(value = "age" ,required = true) @NotNull(message = "id不能為空") @Range(message = "age的長度范圍為{min}歲到{max}歲之間",min = 5,max = 10) private Integer age; @ApiModelProperty(value = "treeNode" ,required = true) @NotEmpty(message = "treeNode不能為空") private List<String> treeNode; }
@Valid 包位置:
import javax.validation.Valid;
@Validated 包的位置
import org.springframework.validation.annotation.Validated; @ApiOperation(value = "新增或者修改一個人的信息") @PostMapping("/updateOrInsert") public Result updateOrInsert(@Valid @RequestBody Person person){ Boolean updateOrInsert = personService.updateOrInsert(person); if (updateOrInsert) { return new Result(ResultCode.SUCCESS,updateOrInsert); } return new Result(ResultCode.ERROR, "新增或者修改一個人的信息失敗"); } @ApiOperation(value = "新增或者修改一個人的信息") @PostMapping("/updateOrInsert") public Result updateOrInsert(@Validated @RequestBody Person person){ Boolean updateOrInsert = personService.updateOrInsert(person); if (updateOrInsert) { return new Result(ResultCode.SUCCESS,updateOrInsert); } return new Result(ResultCode.ERROR, "新增或者修改一個人的信息失敗"); }
最上面三個注釋: 必須需要搭配@Valid 或者@Validated使用,在檢驗Controller的入參是否符合規(guī)范時
@Valid 和 @Validated 比較
最后我們來對 @Valid 和 @Validated 兩個注解進行總結下:
1:@Valid 和 @Validated 兩者都可以對數據進行校驗,待校驗字段上打的規(guī)則注解(@NotNull, @NotEmpty等)都可以對 @Valid 和 @Validated 生效;
2:@Valid 進行校驗的時候,需要用 BindingResult 來做一個校驗結果接收。當校驗不通過的時候,如果手動不 return ,則并不會阻止程序的執(zhí)行;
3:@Validated 進行校驗的時候,當校驗不通過的時候,程序會拋出400異常,阻止方法中的代碼執(zhí)行,這時需要再寫一個全局校驗異常捕獲處理類,然后返回校驗提示。
4:總體來說,@Validated 使用起來要比 @Valid 方便一些,它可以幫我們節(jié)省一定的代碼,并且使得方法看上去更加的簡潔。
此包下其它常用的校驗注解:
注解 | 含義 |
---|---|
@Null | 元素必須為null |
@NotNull | 元素不能null |
@AssertTrue | 元素必須為true |
@AssertFalse | 元素必須是false |
@Min(value) | 元素必須是一個數字,其值必須大于等于指定的最小值 |
@Max(value) | 元素必須是一個數字,其值必須小于等于指定的最大值 |
@DecimalMin(value) | 元素必須是一個數字,其值必須大于等于指定的最小值 |
@DecimalMax(value) | 元素必須是一個數字,其值必須小于等于指定的最大值 |
@Size(max,min) | 元素的大小必須在指定的范圍內 |
@Digits(integer,fraction) | 元素必須是一個數字,其值必須在可接受的范圍內 |
@Past | 元素必須是一個過去的日期 |
@Future | 元素必須是一個將來的日期 |
@Pattern(value) | 元素必須符合指定的正則表達式 |
@Length @NotEmpty @Range | 1:元素必須是電子郵箱地址 2:字符串的大小必須在指定的范圍內 3:字符串必須非空 4:元素必須在合理的范圍內 |
到此這篇關于淺談Spring中@NotEmpty、@NotBlank、@NotNull區(qū)別的文章就介紹到這了,更多相關Spring @NotEmpty、@NotBlank、@NotNull內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring MVC打印@RequestBody、@Response日志的方法
這篇文章主要介紹了Spring MVC打印@RequestBody、@Response日志的方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02Java的PriorityBlockingQueue優(yōu)先級阻塞隊列代碼實例
這篇文章主要介紹了Java的PriorityBlockingQueue優(yōu)先級阻塞隊列代碼實例,PriorityBlockingQueue顧名思義是帶有優(yōu)先級的阻塞隊列,為了實現按優(yōu)先級彈出數據,存入其中的對象必須實現comparable接口自定義排序方法,需要的朋友可以參考下2023-12-12