com.fasterxml.jackson.databind.exc.InvalidDefinitionException - PoolC/Mincho GitHub Wiki
Post์ ๊ด๋ จ๋ ์ธ์ํ ์คํธ ๋์ค์ ํด๋น ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค. Post์ ์กฐํํ๋ ์ธ์ํ ์คํธ์ธ๋ฐ
@Test
void ๋ก๊ทธ์ธxPUBLIC๊ฒ์๋ฌผ์กฐํ() {
ExtractableResponse<Response> response = getPostRequestNoLogin(noticePostId);
PostResponse responseBody = response.as(PostResponse.class);
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value());
assertThat(responseBody.getTitle()).isEqualTo("test1");
}
PostResponse responseBody = response.as(PostResponse.class); ์ด ๋ถ๋ถ์์ ๋ค์๊ณผ ๊ฐ์ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of
org.poolc.api.post.dto.PostResponse (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: (String)"{"postId":9,"memberUuid":"199c5333-967b-4589-96af-26b89b6d0ff0","memberName":"MEMBER_NAME3","title":"test1","body":"test1","createdAt":"2021-02-25T15:39:30.993021","comments":[],"commentCount":0}"; line: 1, column: 2]
Json์ ์๋ฐ ์ค๋ธ์ ํธ๋ก ๋ฐ์๋ค์ด๋ ๊ณผ์ ์์ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค. ์ด ์๋ฌ๊ฐ ๋ฐ์ํ์ ๋, dto PostResponse๋ ๋ค์๊ณผ ๊ฐ๋ค.
@Getter
public class PostResponse {
private final Long postId;
private final String memberUuid;
private final String memberName;
private final String title;
private final String body;
private final LocalDateTime createdAt;
private final List<Comment> comments;
private final Long commentCount;
public PostResponse(Post post) {
this.postId = post.getId();
this.memberUuid = post.getMember().getUUID();
this.memberName = post.getMember().getName();
this.title = post.getTitle();
this.body = post.getBody();
this.createdAt = post.getCreatedAt();
this.comments = post.getCommentList();
this.commentCount = (long) this.comments.size();
}
}
์์ฑ์๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ๋ฃ์ด์คฌ์ ๋ ์๋ฌ๊ฐ ํด๊ฒฐํ๋ค. ์ด๋ @JsonCreator๋ฅผ ๋ฃ์ง ์์๋ ๋ฌธ์ ๋ฅผ ํด๊ฒฐ๋์๋ค. Jackson์ ๊ด๋ จ๋ ์๋ฌ์ธ๋ฐ, @JsonCreator์ ์ญํ ์ ๋ญ๊น?
@Getter
public class PostResponse {
private final Long postId;
private final String memberUuid;
private final String memberName;
private final String title;
private final String body;
private final LocalDateTime createdAt;
private final List<Comment> comments;
private final Long commentCount;
@JsonCreator
public PostResponse(Long postId, String memberUuid, String memberName, String title, String body, LocalDateTime createdAt, List<Comment> comments, Long commentCount) {
this.postId = postId;
this.memberUuid = memberUuid;
this.memberName = memberName;
this.title = title;
this.body = body;
this.createdAt = createdAt;
this.comments = comments;
this.commentCount = commentCount;
}
public PostResponse(Post post) {
this.postId = post.getId();
this.memberUuid = post.getMember().getUUID();
this.memberName = post.getMember().getName();
this.title = post.getTitle();
this.body = post.getBody();
this.createdAt = post.getCreatedAt();
this.comments = post.getCommentList();
this.commentCount = (long) this.comments.size();
}
}