@Builder 어노테이션이 그렇게 편하다고 해서 한번 사용하기로 했다.
생성자로 많은 파라미터를 받을 때,
하지만 @Builder 어노테이션을 사용하려면 기본적으로 생성자를 생성하는 과정이 필요하다.
@AllArgsConstructor
나 @NoArgsConstructor
처럼.
다음은 문제가 되었던 나의 코드이다.
@Getter
@Builder
//DTO 정의
public class MemberSaveDto {
public MemberSaveDto(){
}
@NotBlank
@JsonProperty("user_name")
private String userName;
@EnumValid(enumClass = LoginType.class)
@JsonProperty("login_type")
private LoginType loginType; //회원가입은 논소셜만 가정
@NotBlank
@JsonProperty("user_email")
@Email(message="이메일 주소 양식을 확인해주세요")
private String userEmail; // 소셜 로그인 이메일, 개인 인증 이메일 둘다 user_email에 속함
@NotBlank
@JsonProperty("user_pw")
// @Pattern(regexp=("^(?=.*?[0-9])(?=.*?[#?!@$ %^&*-]).{8,20}$/"),message="비밀번호는 특수문자와 숫자가 적어도 하나 포함된 8~20자리로 설정해주세요")
private String userPw;
}
보면 파라미터가 없는 생성자를 선언한 것이 문제가 된다.
@Builder 어노테이션은 생성자의 유무에 따라 다음과 같이 작동한다고 한다.
사실 전체 파라미터를 받는 생성자 없이 Builder패턴을 적용시키는 것은 말이 안되긴 한다.
따라서, 저 부분에서 @AllArgsConstructor를 붙여주면 정상 작동한다.
정리하면
@NoArgsConstructor
와 @AllArgsConstructor
를 같이 사용하자.
다음은 해당 어노테이션의 실제 설명이다. 중요한 부분은 볼드체로 쳐놨으니 저것만 자세히 볼것!
The builder annotation creates a so-called 'builder' aspect to the class that is annotated or the class that contains a member which is annotated with @Builder.
If a member is annotated, it must be either a constructor or a method. If a class is annotated, then a package-private constructor is generated with all fields as arguments (as if @AllArgsConstructor(access = AccessLevel.PACKAGE) is present on the class), and it is as if this constructor has been annotated with @Builder instead. Note that this constructor is only generated if you haven't written any constructors and also haven't added any explicit @XArgsConstructor annotations. In those cases, lombok will assume an all-args constructor is present and generate code that uses it; this means you'd get a compiler error if this constructor is not present.
The effect of @Builder is that an inner class is generated named TBuilder, with a private constructor. Instances of TBuilder are made with the method named builder() which is also generated for you in the class itself (not in the builder class).
The TBuilder class contains 1 method for each parameter of the annotated constructor / method (each field, when annotating a class), which returns the builder itself. The builder also has a build() method which returns a completed instance of the original type, created by passing all parameters as set via the various other methods in the builder to the constructor or method that was annotated with @Builder. The return type of this method will be the same as the relevant class, unless a method has been annotated, in which case it'll be equal to the return type of that method.
개발자로서 성장하는 데 큰 도움이 된 글이었습니다. 감사합니다.