• Spring注解@NoNull、@NotEmpty、@NotBlank的区别及注解无效以及嵌套对象的无效解决办法


    1.三者的区别
    @NotNull:
    不能为null,但可以为empty(“”," “,” “) ,一般用在基本数据类型的非空校验上,而且被其标注的字段可以使用 @size/@Max/@Min对字段数值进行大小的控制
    @NotEmpty:
    不能为null,而且长度必须大于0(” “,” "),一般用在集合类上面
    @NotBlank:
    不能为null,注意是只能用在String上,而且调用trim()后,长度必须大于0

    简述三者区别
    @NotNull://CharSequence, Collection, Map 和 Array 对象不能是 null, 但可以是空集(size = 0)。
    @NotEmpty://CharSequence, Collection, Map 和 Array 对象不能是 null 并且相关对象的 size 大于 0。
    @NotBlank://String 不能是 null 且去除两端空白字符后的长度(trimmed length)大于 0。

    2.失效问题
    (1)单个对象无效,必须在controller中加上@Valid 或者@Validated。@Validated要和@NotEmpty组合使用

    import org.springframework.validation.annotation.Validated;
     
    public ResultModel test(@Validated @RequestBody TestParam testParam){
    }
    
    • 1
    • 2
    • 3
    • 4

    (2)嵌套对象,对象的对象中失效解决办法,需要在外层对象的属性上加@Valid

    import org.springframework.validation.annotation.Validated;
    import javax.validation.constraints.NotBlank;
     
    @Data
    public class TestParam {
       @NotBlank
       private String className;
     
       @Validated
       private List<UserDTO> users;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    import javax.validation.constraints.NotBlank;
     
    @Data
    public class UserDTO {
        @NotBlank
        private String name;
     
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    isEmpty 和 isBlank 区别
    org.apache.commons.lang3.StringUtils 类提供了 String 的常用操作,最为常用的判空有如下两种 isEmpty(String str) 和 isBlank(String str)。

    分析
    我们通过源码来分析区别:

    /**
    * 

    Checks if a CharSequence is empty ("") or null.

    * *
    * StringUtils.isEmpty(null)      = true
    * StringUtils.isEmpty("")        = true
    * StringUtils.isEmpty(" ")       = false
    * StringUtils.isEmpty("bob")     = false
    * StringUtils.isEmpty("  bob  ") = false
    * 
    */
    public static boolean isEmpty(final CharSequence cs) { return cs == null || cs.length() == 0; } /** *

    Checks if a CharSequence is not empty ("") and not null.

    * *
    * StringUtils.isNotEmpty(null)      = false
    * StringUtils.isNotEmpty("")        = false
    * StringUtils.isNotEmpty(" ")       = true
    * StringUtils.isNotEmpty("bob")     = true
    * StringUtils.isNotEmpty("  bob  ") = true
    * 
    */
    public static boolean isNotEmpty(final CharSequence cs) { return !isEmpty(cs); } /** *

    Checks if a CharSequence is empty (""), null or whitespace only.

    * *

    Whitespace is defined by {@link Character#isWhitespace(char)}.

    * *
     * StringUtils.isBlank(null)      = true
     * StringUtils.isBlank("")        = true
     * StringUtils.isBlank(" ")       = true
     * StringUtils.isBlank("bob")     = false
     * StringUtils.isBlank("  bob  ") = false
     * 
    * */
    public static boolean isBlank(final CharSequence cs) { final int strLen = length(cs); if (strLen == 0) { return true; } for (int i = 0; i < strLen; i++) { if (!Character.isWhitespace(cs.charAt(i))) { return false; } } return true; } /** *

    Checks if a CharSequence is not empty (""), not null and not whitespace only.

    * *

    Whitespace is defined by {@link Character#isWhitespace(char)}.

    * *
     * StringUtils.isNotBlank(null)      = false
     * StringUtils.isNotBlank("")        = false
     * StringUtils.isNotBlank(" ")       = false
     * StringUtils.isNotBlank("bob")     = true
     * StringUtils.isNotBlank("  bob  ") = true
     * 
    * */
    public static boolean isNotBlank(final CharSequence cs) { return !isBlank(cs); }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71

    可以看到:

    StringUtils.isEmpty(final CharSequence cs) 判断某字符串是否为空,为空的标准是 cs == null 或者cs.length() == 0
    StringUtils.isBlank(final CharSequence cs) 判断某字符串是否为空或长度为 0 或由空白符 (whitespace) 构成
    StringUtils.isNotEmpty(final CharSequence cs) 等价于 !isEmpty(final CharSequence cs)
    StringUtils.isNotBlank(final CharSequence cs) 等价于 !isBlank(final CharSequence cs), 我自己更喜欢使用StringUtils.isBlank(final CharSequence cs)来执行判空操作,因为判断的条件更多更具体,特别是进行参数校验时,推荐使用。

  • 相关阅读:
    网页设计成品DW静态网页Html5响应式css3——电影网站bootstrap制作(4页)
    从零学算法(LCR 157)
    【vite】vite.defineConfig is not a function/npm无法安装第三方包问题
    深入剖析Sgementation fault原理
    PIMPL技巧
    安装element-plus
    MIT6.830-2022-lab1实验思路详细讲解
    C语言中的3种注释方法
    【博学谷学习记录】超强总结,用心分享|架构师-Netty核心组件
    记录将excel表无变形的弄进word里面来
  • 原文地址:https://blog.csdn.net/qq_44543774/article/details/136349146