Jakarta Bean Validation 2.0 中有三个内置的空值相关的约束Annotation。
@NotNull
@NotEmpty
@NotBlank
这里简单展示一下他们的区别。
@NotNull
@NotNull
String abc;
-------------------
abc = null; // @NotNull will be triggered.
abc = ""; // @NotNull is okay with "".
abc = " "; // @NotNull is okay with " ".
@NotEmpty
@NotEmpty
String abc;
-------------------
abc = null; // @NotEmpty will be triggered.
abc = ""; // @NotEmpty will be triggered.
abc = " "; // @NotEmpty is okay with " ".
@NotBlank
@NotBlank
String abc;
------------------
abc = null; // @NotBlank will be triggered.
abc = ""; // @NotBlank will be triggered.
abc = " "; // @NotBlank will be triggered.
abc ="1"; // @NotBlank is okay with "1"; non-space, visible characters.