Springboot中注解@NotNull,@NotBlank,@NotEmpty的使用区别

 

import org.hibernate.validator.constraints.NotBlank;

import org.hibernate.validator.constraints.NotEmpty; 

import javax.validation.constraints.NotNull;

@NotNull:多用在基本数据类型上(Int,Integer,Double)

举例:

@ApiModelProperty("年龄")

@NotNull(message = "年龄不能为空!") 

private Integer age;

 

@NotBlank:多用在String字符串上面(String)

举例:

@ApiModelProperty("用户代码")

@NotBlank(message = "用户代码不能为空!") 

private String userCode;

 

@NotEmpty; 加了@NotEmpty注解的String类 ,Collection集合,Map ,数组,这些是不能为null或者长度为0的;(String ,Collection,Map的isEmpty()方法)

@ApiModelProperty("学生") 

@NotEmpty(message = "至少有一个学生!") 

private List<String> stdId;

 

主要判断对象:

1.@NotNull

源码解释:

The annotated element must not be {@code null}.

带此注解的元素不能是null。

2.@NotEmpty

源码解释:

Asserts that the annotated string, collection, map or array is not {@code null} or empty.

断言带此注解的字符串、集合、映射或数组不是null或为空。

即,不为null,不为空!如在List上则该列表的size不为0。

3.@NotBlank

源码解释:

Validate that the annotated string is not {@code null} or empty.

The difference to {@code NotEmpty} is that trailing whitespaces are getting ignored.

断言带此注解的字符串、集合、映射或数组不是null或为空,与@NotEmpty注解不同的是末尾的空格会被忽略!

即,不为null,不为空,不为空格!

 

说明:

1、上述的空(empty) 为 没有该属性即前端不传该属性!

三者断言的范围大小排序:

@NotNull < @NotEmpty < @NotBlank

2、若要使上述的@NotNull , @NotEmpty , @NotBlank注解生效,

需要在使用到的实体类前加上@Valid注解,如下:

@RequestMapping(value = {"/add"}, method = {RequestMethod.POST})

@ApiOperation(value = "新增", notes = "新增") 

public AddParam add(@RequestBody @Valid AddParam addParam) {...}

 

 

 

回答: 在Spring Boot中,如果你想要对一个List类型的参数进行非空校验,你可以使用@NotNull注解。\[2\]在你的Controller层的方法参数上添加@Valid注解,这样就可以触发参数校验。同时,在pom.xml文件中添加spring-boot-starter-validation依赖,以确保校验功能可用。\[1\]这样,当你的接口被调用时,如果传入的List参数为空,就会触发校验错误。这样可以确保参数的准确性和完整性。 #### 引用[.reference_title] - *1* [18.日常问题整理[2022/7/26]【SpringBoot】校验-@valid、@NotBlank@NotEmpty@NotNull注解使用场景及...](https://blog.csdn.net/weixin_46876034/article/details/125979214)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [SpringBoot注解@NotBlank@NotNull@NotEmpty不生效的原因](https://blog.csdn.net/Lazy_Goat/article/details/118890847)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [springboot注解@NotNull@NotBlank,@Valid自动判定空值](https://blog.csdn.net/lisheng19870305/article/details/114442126)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值