@validate校验中的@NotEmpty、@NotBlank、@NotNull区别

1、引入的包:jakarta.validation-api-2.0.2.jar

在这里插入图片描述

2、直接看源码上的注释

@NotEmpty
/**
 * The annotated element must not be {@code null} nor empty.
 * 

* Supported types are: *

    *
  • {@code CharSequence} (length of character sequence is evaluated)
  • *
  • {@code Collection} (collection size is evaluated)
  • *
  • {@code Map} (map size is evaluated)
  • *
  • Array (array length is evaluated)
  • *
* * @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 { ... }
解释
  • 此注解元素校验必须不为null且不为空
  • 可以修饰的数据类型:
  • CharSequence(字符串)
  • Collection(集合)
  • Map(键值对)
  • Array(数组)
@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 {
	...
}
解释
  • 此注解元素校验必须不为null且必须包含一个不为空的字符串
  • 只可以修饰的数据类型:CharSequence(字符串)
@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 {
	...
}
解释
  • 此注解元素校验必须不为null
  • 接受任意类型的数据

3、用法示例

    /**
     * 员工ID
     */
    @NotEmpty(message = "员工id不能为空")
    private List<Integer> empIdList;


    /**
     * 客户id
     */
    @NotBlank(message = "公司名称不能为空")
    private String companyName;

    /**
     * 组织id
     */
    @NotNull(message = "组织ID不能为空")
    private String orgId;

你可能感兴趣的:(java,spring,开发语言)