반응형
DTO의 불변성을 위해 보통 아래와 같이 DTO를 만듭니다
@Builder
@Getter
public class TestDTO {
private final String name;
}
간혹 Jackson에서 역직렬화를 하는경우가 있는데 이럴경우 해당 클래스는 기본 생성자가 없으므로 JSON parse error: Cannot construct instance of 가 발생합니다.
해결방법은 여러가지가 있지만 아래아 같이 처리할수 있습니다
1) JsonCreator 와 JsonProperty 사용하여 생성자 만들기
private @JsonCreator TestDTO(
@JsonProperty("name") String name) {
this.name = name;
}
TestDTO 클래스에 위와 같이 생성자를 추가해 줍니다
2) Jacksonized 사용하기
@Builder
@Jacksonized
@Getter
public class TestDTO {
private final String name;
}
DTO클래스에 @Jacksonized 를 추가해줍니다. 해당 어노테이션은 Builder의 추가기능으로 역직렬화를 할수 있게 해줍니다
3) Jackson 사용하지 않기
@SpringBootApplication(exclude = { JacksonAutoConfiguration.class })
public class TestApplication {
}
반응형
'Spring' 카테고리의 다른 글
[Spring] HikariPool-1 - Failed to validate connection ConnectionID (0) | 2023.05.11 |
---|---|
[Spring JPA] Executing an update/delete query (0) | 2023.04.28 |
[Spring JPA] 외래키를 복합키에 포함하기 (0) | 2023.03.31 |
Gradle (0) | 2023.03.29 |
[Spring] mapStruct에서 LocalDateTime을 epoch Milli로 변경하는 방법 (0) | 2023.03.24 |
댓글