JSON ↔ DTO 매핑 조건

프론트에서 보내는 JSON:

{
  "email": "[email protected]",
  "password": "password123",
  "nickname": "홍길동",
  "phoneNumber": "01012345678"
}

이 JSON이 Spring Boot의 DTO로 매핑되기 위해서는 필드 이름이 DTO와 정확히 일치해야 자동 매핑 됨

public class UserSignUpRequestDTO {
    private String email;
    private String password;
    private String nickname;
    private String phoneNumber;
    ...
}

JSON 필드 이름이 다르더라도 매핑

프론트에서 JSON이 이렇게 오면:

{
  "phoneNumber": "01012345678"
}

Jackson의 @JsonProperty를 사용

public class UserSignUpRequestDTO {
    @JsonProperty("phoneNumber")
    private String phone_num;

    // Getter, Setter 필요!
    public String getPhone_num() {
        return phone_num;
    }

    public void setPhone_num(String phone_num) {
        this.phone_num = phone_num;
    }
}

DTO ↔ Entity 매핑 조건

DTO와 Entity는 반드시 필드 이름이 똑같을 필요는 없습니다.

왜냐하면: