[Spring boot] @NotNull, @NotEmpty, @NotBlank 차이

SMongS·2022년 9월 15일
0

Spring boot

목록 보기
2/6

개발하면서 API의 파라미터 요청할때, null을 체크해야 합니다.
Spring boot를 사용하면서 null 체크를 할때 사용되는 것들이 3가지 정도 있었습니다.

@NotNull, @NotEmpty, @NotBlank인데,
이 3가지의 어노테이션은 Bean Validation에서 제공합니다.

NotNull

  • null을 허용하지 않습니다.
  • "" 이나 " " 은 허용하게 됩니다.

NotEmpty

  • 주석이 달린 요소가 null 또는 비어 있는지 확인합니다.
  • null 과 "" 둘 다 허용하지 않게 합니다.
  • " "은 허용하게 됩니다.

NotBlank

  • 빈칸 혹은 공백만 있는 경우를 허용하지 않습니다.
  • null 과 "" 과 " " 모두 허용하지 않습니다.

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

다양한 속성이나 기능 확인 (@Size, @Min, @Max,...) : https://beanvalidation.org/2.0/spec/

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

profile
반갑습니당~😄

0개의 댓글