[트러블슈팅/SpringBoot] Builder 필드 초기값 누락

2026. 1. 22. 15:30·트러블슈팅

문제

로그인 기능 구현 후 테스트를 진행하는데, 로그인 성공 시 반환되는 AuthResponse에서 type 값이 null로 나옴

필드 초기값을 설정했는데 왜 null로 나오는지 찾아봤다

원인

Lombok의@Builder는 필드 초기값을 전혀 사용하지 않는다고 함

아래와 같이 AuthResponse 필드에 초기값을 넣어놔도 이는 기본 생성자(new AuthResponse())를 사용할 때만 적용됨

// 인증 응답 DTO
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class AuthResponse {

    private String token;
    private String type = "Bearer";    // 초기값 설정
    private Long id;
    private String username;
    private String email;

    public static AuthResponse of(String token, Long id, String username, String email) {
        return AuthResponse.builder()
                .token(token)
                .type("Bearer")
                .id(id)
                .username(username)
                .email(email)
                .build();
    }

}

@Builder는 내부적으로 AllArgsConstructor를 호출하기 때문에, 어디선가 .type()이 안 들어간 Builder가 호출되면 type = null로 적용

해결

@Builder.Default를 사용하여 기본값 유지

@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class AuthResponse {

    private String token;

    @Builder.Default    // 추가한 부분
    private String type = "Bearer";

    private Long id;
    private String username;
    private String email;

    public static AuthResponse of(String token, Long id, String username, String email) {
        return AuthResponse.builder()
                .token(token)
//              .type("Bearer")	생략 가능
                .id(id)
                .username(username)
                .email(email)
                .build();
    }

}

 

결과

type값이 잘 나오는 것을 확인할 수 있음

저작자표시 (새창열림)

'트러블슈팅' 카테고리의 다른 글

[트러블슈팅/SpringBoot] 415 오류: Content-Type 'application/octet-stream' is not supported  (0) 2026.05.19
[트러블슈팅/SpringBoot] WebMvcTest로 Controller 테스트 시 JwtUtil 인식 안 됨  (1) 2026.03.11
[트러블슈팅/GitHub] 원격저장소 - 로컬저장소 기록(history) 병합  (0) 2026.01.17
[트러블슈팅/SpringBoot] Error creating bean with name 'entityManagerFactory' defined in class path resource  (0) 2024.12.05
[트러블슈팅/Java] incompatible types: char cannot be converted to string  (0) 2024.07.04
'트러블슈팅' 카테고리의 다른 글
  • [트러블슈팅/SpringBoot] 415 오류: Content-Type 'application/octet-stream' is not supported
  • [트러블슈팅/SpringBoot] WebMvcTest로 Controller 테스트 시 JwtUtil 인식 안 됨
  • [트러블슈팅/GitHub] 원격저장소 - 로컬저장소 기록(history) 병합
  • [트러블슈팅/SpringBoot] Error creating bean with name 'entityManagerFactory' defined in class path resource
naahy
naahy
  • naahy
    종합장
    naahy
  • 전체
    오늘
    어제
    • 분류 전체보기
      • Java
      • SpringBoot
      • Git
      • 트러블슈팅
      • Vue.js
      • Kotlin
      • Node.js
      • 코딩테스트
        • 백준
        • 프로그래머스
      • 스터디
        • CS
        • 알고리즘
      • -
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 인기 글

  • 태그

    SpringBoot
    Kotlin
    Nexacro
    Java
    cs
    node.js
    브루트포스
    API
    백준
    백트래킹
    이분탐색
    github
    투포인터
    mongodb
    bfs
    코틀린
    자바
    프로그래머스
    트러블슈팅
    React
  • 최근 글

  • hELLO· Designed By정상우.v4.10.6
naahy
[트러블슈팅/SpringBoot] Builder 필드 초기값 누락
상단으로

티스토리툴바