In Hibernate Validator 4.1+, what is the difference between @NotNull, @NotEmpty and @NotBlank?

I can't seem to find a resume that distinguishes the difference between the three annotations.

+104
java validation hibernate bean-validation
Jun 16 '13 at 20:18
source share
3 answers

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

To help you understand, let's look at how these restrictions are defined and implemented (I am using version 4.1):

  • The @NotNull is defined as:

     @Constraint(validatedBy = {NotNullValidator.class}) 

    This class has an isValid method defined as:

     public boolean isValid(Object object, ConstraintValidatorContext constraintValidatorContext) { return object != null; } 
  • The @NotEmpty is defined as:

     @NotNull @Size(min = 1) 

    Thus, this restriction uses the @NotNull restriction above, and @Size , whose definition is different from the object, but must be self-learning.

  • Finally, the @NotBlank constraint @NotBlank defined as:

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

    Thus, this restriction also uses the @NotNull restriction, but also restricts the NotBlankValidator class. This class has an isValid method defined as:

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

    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 is normal that it returns true if it is null, because, as I mentioned, the definition of @NotEmpty also requires @NotNull .

Here are some examples:

  • String name = null;
    @NotNull : false
    @NotEmpty : false
    @NotBlank : false

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

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

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

+288
Jun 16 '13 at 20:18
source share

I liked the explanation in the link below: http://www.itprogrammingtutorials.com/2015/java/hibernate/hibernate-validator-diff-notblank-notempty/

@NotNull: checks if a value is null, not considering the contents

@NotEmpty: checks if a value is non-null and non-null. If there are only empty spaces in it, it will be resolved as not empty.

@NotBlank: checks if a value is null or empty by trimming the value first. This means that it does not allow empty spaces.

So, if you want to check that the field is not empty, and also that it has no empty spaces, but contains text, you should use @NotBlank.

+7
Mar 06 '17 at 17:16
source share
  1. @NotNull: limited sequence of CharSequence, Collection, Map, or Array is valid if it is not non-zero, but may be empty
  2. @NotEmpty: Limited CharSequence, Collection, Map or Array is valid if it is not equal to zero and its size / length is greater than zero.
  3. @NotBlank: bounded string is valid if it is not equal to zero and the truncated length is greater than zero.
0
Jul 03 '19 at 7:32
source share



All Articles