Excerpt: https: //www.cnblogs.com/Terry-Wu/p/8134732.html
Example:
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
Description of the three differences
@NotNull: // CharSequence, Collection, Map Array object and can not be null, but may be an empty set (size = 0). @NotEmpty: // CharSequence, Collection, Map and Array objects can not be null and size related objects greater than zero. @NotBlank: // String is not null and the length removed (trimmed length) after the two ends of the blank character is greater than 0.
Defined annotations (in version 4.1 in):
1, @ NotNull:
It is defined as follows:
@Constraint(validatedBy = {NotNullValidator.class})
This class has a method isValid is so defined:
public boolean isValid(Object object, ConstraintValidatorContext constraintValidatorContext) { return object != null; }
Object is not null on the line, the other is not guaranteed.
2、@NotEmpty:
It is defined as follows:
@NotNull @Size(min = 1)
也就是说,@NotEmpty除了@NotNull之外还需要保证@Size(min=1),这也是一个注解,这里规定最小长度等于1,也就是类似于集合非空。
3、@NotBlank:
@NotNull @Constraint(validatedBy = {NotBlankValidator.class})
Similarly, in addition to @NotNull, there is defined a class, the class also has isValid method:
if ( charSequence == null ) { //curious return true; } return charSequence.toString().trim().length() > 0;
Interestingly, it returns true if the object is a null string when a method, but only if and when it returns false trimmed length of zero. Returns true even when the string is null when the method, but also contains @NotNull since @NotBlank, so @NotBlank claim string is not null.