@NotNull, @NotEmpty, @NotBlank
@NotNull @NotEmpty @NotBlank
javax.validation.constraints package에 포함된 기능으로 api에서 값을 입력받을 때 validation 체크를 위해 사용되는 어노테이션입니다.
많이 사용하게 되는 어노테이션으로 한 번만 차이를 확실히 알고 나면 용도에 맞게 잘 사용할 수 있습니다.
@NotNull
The annotated element must not be null. Accepts any type.
NotNull은 말 그대로 null 값만 허용하지 않습니다. 그렇기 때문에 "", " " 가 입력되었을 경우는 허용하게 됩니다.
@NotEmpty
@NotEmpty
The annotatd 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)
NotEmpty는 null과 추가로 "" 입력도 허용하지 않습니다. 다만 " " 의 입력은 허용됩니다.
@NotBlank
The annotated element must not be null and must contain at least one non-whitespace character. Accepts CharSequence.
NotBlank는 세 가지 어노테이션 중 가장 강도가 강한 것으로 null, "", " " 모두 허용하지 않습니다.
* whitespace = 공백 형태로 된 문자
String text = null;
@NotNull = false
@NotEmpty = false
@NotBlank = false
String text = "";
@NotNull = true
@NotEmpty = false
@NotBlank = false
String text = " "; // whitespace
@NotNull = true
@NotEmpty = true
@NotBlank = false