Java

@NotNull @NotEmpty @NotBlank 차이

일태우 2020. 3. 4. 15:01
반응형

Bean Validation API의 NotNull, NotEmpty, NotBlank의 차이에 대해 설명한다.

 

@NotNull

The annotated element must not be null. Accepts any type.

모든 타입에 대해 null을 허용하지 않는다.

 

@NotEmpty

The annotated element must not be null nor empty. Supported types are:

  • CharSequence (length of character sequence is evaluated)
  • Collection (collection size is evaluated)
  • Map (map size is evaluated)
  • Array (array length is evaluated)

위의 나열된 타입에 대해 null, 빈 값을 허용하지 않는다.

예로 boolean 타입의 경우는 NotEmpty를 적용할 수 없다.

 

@NotBlank

The annotated element must not be null and must contain at least one non-whitespace character. Accepts CharSequence.

CharSequence 타입에 대해 null을 허용하지 않고, 최소 한 개 이상의 공백 문자가 아닌 문자를 포함해야 한다.

(String은 CharSequence 구현체이므로 해당된다.)

 

참고

https://javaee.github.io/javaee-spec/javadocs/javax/validation/constraints/package-summary.html

https://docs.oracle.com/javase/8/docs/api/java/lang/String.html

반응형