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();
    }
}
โš ๏ธ **GitHub.com Fallback** โš ๏ธ