NotNull、NotEmpty、NotBlank的区别

@NotNull、@NotEmpty、@NotBlank的区别

大致区别如下:

@NotEmpty用在字符串、集合、数组上面 @NotBlank 用在String上面 @NotNull 用在任何类型上

@NotEmpty

/**
* The annotated element must not be {@code null} nor empty.
* <p>
* Supported types are:
* <ul>
* <li>{@code CharSequence} (length of character sequence is evaluated)</li>
* <li>{@code Collection} (collection size is evaluated)</li>
* <li>{@code Map} (map size is evaluated)</li>
* <li>Array (array length is evaluated)</li>
* </ul>
*
* @author Emmanuel Bernard
* @author Hardy Ferentschik
*
* @since 2.0
*/
@Documented
@Constraint(validatedBy = { })
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
@Retention(RUNTIME)
@Repeatable(List.class)
public @interface NotEmpty {

   String message() default "{jakarta.validation.constraints.NotEmpty.message}";

   Class<?>[] groups() default { };

   Class<? extends Payload>[] payload() default { };

   /**
    * Defines several {@code @NotEmpty} constraints on the same element.
    *
    * @see NotEmpty
    */
   @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
   @Retention(RUNTIME)
   @Documented
   public @interface List {
   	NotEmpty[] value();
   }
}

@NotEmpty 可以用在字符串 集合 Map 数组 上,被注解的元素不能为 null 也不能为空。

@NotBlank


/**
* The annotated element must not be {@code null} and must contain at least one
* non-whitespace character. Accepts {@code CharSequence}.
*
* @author Hardy Ferentschik
* @since 2.0
*
* @see Character#isWhitespace(char)
*/
@Documented
@Constraint(validatedBy = { })
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
@Retention(RUNTIME)
@Repeatable(List.class)
public @interface NotBlank {

   String message() default "{jakarta.validation.constraints.NotBlank.message}";

   Class<?>[] groups() default { };

   Class<? extends Payload>[] payload() default { };

   /**
    * Defines several {@code @NotBlank} constraints on the same element.
    *
    * @see NotBlank
    */
   @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
   @Retention(RUNTIME)
   @Documented
   public @interface List {
   	NotBlank[] value();
   }
}

@NotBlank 只能用在 字符串 上,被注解的元素不能为null且至少包含一个非空白字符。

@NotNull


/**
* The annotated element must not be {@code null}.
* Accepts any type.
*
* @author Emmanuel Bernard
*/
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
@Retention(RUNTIME)
@Repeatable(List.class)
@Documented
@Constraint(validatedBy = { })
public @interface NotNull {

  String message() default "{jakarta.validation.constraints.NotNull.message}";

  Class<?>[] groups() default { };

  Class<? extends Payload>[] payload() default { };

  /**
   * Defines several {@link NotNull} annotations on the same element.
   *
   * @see jakarta.validation.constraints.NotNull
   */
  @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
  @Retention(RUNTIME)
  @Documented
  @interface List {

  	NotNull[] value();
  }
}

@NotNull 能用在任何类型上,被注解的元素不能为 null

总结


1.String name = null;

@NotNull: false
@NotEmpty:false
@NotBlank:false


2.String name = "";

@NotNull:true
@NotEmpty: false
@NotBlank: false

3.String name = " ";

@NotNull: true
@NotEmpty: true
@NotBlank: false

4.String name = "Great answer!";

@NotNull: true
@NotEmpty:true
@NotBlank:true