Spring Bootreqの@ NotNull、@ NotEmpty、@ NotBlankの違いは何ですか?



What Is Difference Between Notnull



3つの違いを簡単に説明してください

@NotNull://CharSequence, Collection, Map and Array objects cannot be null, but can be an empty set (size = 0). @NotEmpty://CharSequence, Collection, Map and Array objects cannot be null and the size of the related object is greater than 0. @NotBlank://String is not null and the length (trimmed length) after removing the whitespace characters is greater than 0.

注釈の定義(バージョン4.1):

1、@ NotNull:



定義は次のとおりです。

@Constraint(validatedBy = {NotNullValidator.class})

このクラスには、次のように定義されたisValidメソッドがあります。



public boolean isValid(Object object, ConstraintValidatorContext constraintValidatorContext) { return object != null }

オブジェクトはnullではなく、もう一方は保証されません。

2、@ NotEmpty:

定義は次のとおりです。



@NotNull @Size(min = 1)

That is to say, @NotEmpty needs to guarantee @Size(min=1) in addition to @NotNull, which is also an annotation. Here, the minimum length is equal to 1, which means that the collection is not empty.

3、@ NotBlank:

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

同様に、@ NotNullに加えて、クラス定義があり、このクラスにもisValidメソッドがあります。

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

Interestingly, the method returns true when a string object is null, but returns false if and only if its trimmed length is equal to zero. This method returns true even when string is null, but since @NotBlank also contains @NotNull, @NotBlank requires string not to be null.

例:

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

First, commonly used verification annotations (1) Commonly used labels @Null The commented element must be null @NotNull The commented element cannot be null @AssertTrue The commented element must be true @AssertFalse The commented element must be false @Min(value) The commented element must be a number whose value must be greater than or equal to the specified minimum @Max(value) The commented element must be a number whose value must be less than or equal to the specified maximum @DecimalMin(value) The commented element must be a number whose value must be greater than or equal to the specified minimum @DecimalMax(value) The commented element must be a number whose value must be less than or equal to the specified maximum @Size(max,min) The size of the commented element must be within the specified range. @Digits(integer,fraction) The commented element must be a number whose value must be within an acceptable range @Past The commented element must be a past date @Future The commented element must be a future date @Pattern(value) The annotated element must conform to the specified regular expression. @Email The commented element must be an email address @Length The size of the commented string must be within the specified range @NotEmpty The commented string must be non-empty @Range The commented element must be in the appropriate range