在 Hibernate Validator 4.1+ 中,@NotNull、@NotEmpty 和 @NotBlank 之间有什么区别? [英] In Hibernate Validator 4.1+, what is the difference between @NotNull, @NotEmpty, and @NotBlank?

查看:21
本文介绍了在 Hibernate Validator 4.1+ 中,@NotNull、@NotEmpty 和 @NotBlank 之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法找到区分这三个注释之间差异的摘要.

I can't seem to be able to find a summary that distinguishes the difference between these three annotations.

推荐答案

@NotNull:CharSequence、Collection、Map 或 Array 对象 is not null,但是 可以为空.
@NotEmpty:CharSequence、Collection、Map 或 Array 对象不为空且大小 > 0.
@NotBlank:字符串不为空并且修剪后的长度大于零.

@NotNull: The CharSequence, Collection, Map or Array object is not null, but can be empty.
@NotEmpty: The CharSequence, Collection, Map or Array object is not null and size > 0.
@NotBlank: The string is not null and the trimmed length is greater than zero.

为了帮助您理解,让我们看看这些约束是如何定义和执行的(我使用的是 4.1 版):

To help you understand, let's look into how these constraints are defined and carried out (I'm using version 4.1):

  1. @NotNull 约束定义为:

@Constraint(validatedBy = {NotNullValidator.class})  

这个类有一个 isValid 方法定义为:

This class has an isValid method defined as:

public boolean isValid(Object object, ConstraintValidatorContext constraintValidatorContext) {
 return object != null;  
}

  • @NotEmpty 约束定义为:

    @NotNull  
    @Size(min = 1)    
    

    所以这个约束使用上面的@NotNull约束, @Size 其定义因对象,但应该是不言自明的.

    So this constraint uses the @NotNull constraint above, and @Size whose definition differs based on the object but should be self explanitory.

    最后,@NotBlank 约束定义为:

    @NotNull  
    @Constraint(validatedBy = {NotBlankValidator.class})        
    

    所以这个约束也使用了 @NotNull 约束,但也用 NotBlankValidator 类进行了约束.这个类有一个 isValid 方法定义为:

    So this constraint also uses the @NotNull constraint, but also constrains with the NotBlankValidator class. This class has an isValid method defined as:

    if ( charSequence == null ) {  //curious 
      return true;   
    }   
    return charSequence.toString().trim().length() > 0;  
    

    有趣的是,如果字符串为空,则此方法返回 true,但当且仅当修剪后的字符串的长度为 0 时返回 false.如果为 null,则返回 true 是可以的,因为正如我提到的,@NotEmpty 定义也需要 @NotNull.

    Interestingly, this method returns true if the string is null, but false if and only if the length of the trimmed string is 0. It's ok that it returns true if it's null because, as I mentioned, the @NotEmpty definition also requires @NotNull.

    这里有几个例子:

    1. 字符串名称 = null;
      @NotNull:假
      @NotEmpty:假
      @NotBlank:假

    字符串名称 = "";
    @NotNull:
    @NotEmpty:假
    @NotBlank:假

    String name = "";
    @NotNull: true
    @NotEmpty: false
    @NotBlank: false

    字符串名称 = " ";
    @NotNull:
    @NotEmpty:
    @NotBlank:假

    String name = " ";
    @NotNull: true
    @NotEmpty: true
    @NotBlank: false

    String name = "很好的答案!";
    @NotNull:
    @NotEmpty:
    @NotBlank: true

    String name = "Great answer!";
    @NotNull: true
    @NotEmpty: true
    @NotBlank: true

    这篇关于在 Hibernate Validator 4.1+ 中,@NotNull、@NotEmpty 和 @NotBlank 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆