avatar

目录
@NotNull、@NotEmpty、@NotBlank和@PositiveOrZero等Spring限制类注解的使用

我们在做web项目的时候经常会遇到前端传参的情况,这会需要对参数做校验,比如参数不能为空,集合不能为空等校验,javax.validation.constraints为我们提供了很多注解来帮忙我们做参数校验。

1、 @NotNull

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

校验参数一定不能为null,但是可以为” “。

2、 @NotEmpty

The annotated element must not be {@code null} nor empty. Supported types are:
1、{@code CharSequence} (length of character sequence is evaluated);
2、{@code Collection} (collection size is evaluated);
3、{@code Map} (map size is evaluated);
4、Array (array length is evaluated);

校验集合类参数(如String类、Collection、Map、数据Array)不能为null或empty。其中String的length、Collection和Map的size不能为0。

3、@NotBlank

The annotated element must not be {@code null} and must contain at least one non-whitespace character. Accepts {@code CharSequence}.

校验String字符串不能为null,且不能是空字符串(” “),即调用trim()之后字符串的长度不能为0。

4、@PositiveOrZero

The annotated element must be a positive number or 0.

校验参数必须是正整数或0。

文章作者: 海东青
文章链接: https://haohaogit.github.io/2020/08/06/NotNull%E3%80%81NotEmpty%E3%80%81NotBlank%E5%92%8CPositiveOrZero%E7%AD%89Spring%E9%99%90%E5%88%B6%E7%B1%BB%E6%B3%A8%E8%A7%A3%E7%9A%84%E4%BD%BF%E7%94%A8/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Hexo
打赏
  • 微信
    微信
  • 支付宝
    支付宝