개발/Spring Boot
-
[Spring Boot] Entity builder 설정하기개발/Spring Boot 2024. 1. 15. 03:05
Spring Boot에서 Entity를 만들면 id와 다른 여러 필드로 구성한다. repository에 save 할 때 Entity의 instance를 만들어야 하는데 Lombok의 @AllArgsConstructor를 통해서 객체를 만들 수 있지만 필드의 순서나 변경이 생기면 관련 로직이 모두 망가지는 문제가 있다. 또, id같이 직접 지정하지 않아도 되는 필드에 임의로 값을 넣어야 하는 불편함도 있다. JPA에 의해서 자동으로 id값이 변경되어 저장되긴 하지만 의도하지 않은 동작을 일으킬 우려가 있다. Lombok에서 제공하는 @Builder를 사용하면 이런 문제를 해결할 수 있다. User 엔티티 필자의 프로젝트인 portpolent의 user 엔티티의 스키마는 다음과 같다. 이름 타입 비고 id ..
-
@NotNull, @NotEmpty, @NotBlank 차이개발/Spring Boot 2023. 12. 14. 23:13
Spring Validation의 NotNull, NotEmpty, NotBlank 등의 검증을 하게 되는데 저 셋의 차이를 잘 모르겠어서 찾아보고 정리한다. null "" " " NotNull X O O NotEmpty X X O NotBlank X X X 다만, @NotBlank를 사용한다고 해도 " hi" 같은 완벽하게 비어있지 않은 문자열은 걸러낼 수 없다. 이런 경우 Pattern을 통해 정규표현식을 사용하거나 다른 방법을 사용해야 한다. 에러 메세지 커스터마이징 하기 validation을 통과하지 못했을 때 반환되는 message는 Bad Request뿐이다. 어떤 필드가 문제가 있는지 알려주고 싶다. 에러 메세지를 커스터마이징 할 수 있는데 자세한 내용은 아래 글을 참고하시라. [Spring..