淺談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
適用于基本數(shù)據(jù)類型(Integer,Long,Double等等),當 @NotNull 注解被使用在 String 類型的數(shù)據(jù)上,則表示該數(shù)據(jù)不能為 Null(但是可以為 Empty)
注:被其標注的字段可以使用 @size、@Max、@Min 對字段數(shù)值進行大小的控制
@NotBlank
適用于 String 類型的數(shù)據(jù)上,加了@NotBlank 注解的參數(shù)不能為 Null 且 trim() 之后 size > 0,必須有實際字符
@NotEmpty
適用于 String、Collection集合、Map、數(shù)組等等,加了@NotEmpty 注解的參數(shù)不能為 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的入?yún)⑹欠穹弦?guī)范時
@Valid 和 @Validated 比較
最后我們來對 @Valid 和 @Validated 兩個注解進行總結(jié)下:
1:@Valid 和 @Validated 兩者都可以對數(shù)據(jù)進行校驗,待校驗字段上打的規(guī)則注解(@NotNull, @NotEmpty等)都可以對 @Valid 和 @Validated 生效;
2:@Valid 進行校驗的時候,需要用 BindingResult 來做一個校驗結(jié)果接收。當校驗不通過的時候,如果手動不 return ,則并不會阻止程序的執(zhí)行;
3:@Validated 進行校驗的時候,當校驗不通過的時候,程序會拋出400異常,阻止方法中的代碼執(zhí)行,這時需要再寫一個全局校驗異常捕獲處理類,然后返回校驗提示。
4:總體來說,@Validated 使用起來要比 @Valid 方便一些,它可以幫我們節(jié)省一定的代碼,并且使得方法看上去更加的簡潔。
此包下其它常用的校驗注解:
| 注解 | 含義 |
|---|---|
| @Null | 元素必須為null |
| @NotNull | 元素不能null |
| @AssertTrue | 元素必須為true |
| @AssertFalse | 元素必須是false |
| @Min(value) | 元素必須是一個數(shù)字,其值必須大于等于指定的最小值 |
| @Max(value) | 元素必須是一個數(shù)字,其值必須小于等于指定的最大值 |
| @DecimalMin(value) | 元素必須是一個數(shù)字,其值必須大于等于指定的最小值 |
| @DecimalMax(value) | 元素必須是一個數(shù)字,其值必須小于等于指定的最大值 |
| @Size(max,min) | 元素的大小必須在指定的范圍內(nèi) |
| @Digits(integer,fraction) | 元素必須是一個數(shù)字,其值必須在可接受的范圍內(nèi) |
| @Past | 元素必須是一個過去的日期 |
| @Future | 元素必須是一個將來的日期 |
| @Pattern(value) | 元素必須符合指定的正則表達式 |
@Length @NotEmpty @Range | 1:元素必須是電子郵箱地址 2:字符串的大小必須在指定的范圍內(nèi) 3:字符串必須非空 4:元素必須在合理的范圍內(nèi) |
到此這篇關(guān)于淺談Spring中@NotEmpty、@NotBlank、@NotNull區(qū)別的文章就介紹到這了,更多相關(guān)Spring @NotEmpty、@NotBlank、@NotNull內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot中@EnableAsync和@Async注解的使用小結(jié)
在SpringBoot中,可以通過@EnableAsync注解來啟動異步方法調(diào)用的支持,通過@Async注解來標識異步方法,讓方法能夠在異步線程中執(zhí)行,本文就來介紹一下,感興趣的可以了解一下2023-11-11
SpringQuartz定時任務核心組件JobDetail與Trigger配置
Spring框架與Quartz調(diào)度器的集成提供了強大而靈活的定時任務解決方案,本文主要介紹了SpringQuartz定時任務核心組件JobDetail與Trigger配置,具有一定的參考價值,感興趣的可以了解一下2025-04-04
elasticsearch通過guice注入Node組裝啟動過程
這篇文章主要為大家介紹了?elasticsearch通過guice注入Node組裝啟動過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-04-04

