프론트에서 보내는 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;
...
}
null
로 들어오거나, 역직렬화 에러 발생 가능"phoneNumber"
인데 DTO가 phone_num
이면 매핑 안 됨프론트에서 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;
}
}
@JsonProperty(”phoneNumber”)
덕분에
→ phoneNumber
라는 JSON 키가
→ phone_num
이라는 Java 필드에 자동으로 매핑된다.
DTO와 Entity는 반드시 필드 이름이 똑같을 필요는 없습니다.
왜냐하면:
ModelMapper
, MapStruct
같은 라이브러리로 매핑하기 때문입니다.