Skip to content

com.fasterxml.jackson.databind.exc.InvalidDefinitionException

hcpak edited this page Feb 25, 2021 · 3 revisions

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();
    }
}

Product Backlog

🕸 Link


Progress

🧑🏻‍💻 회의록
🌳 기획 및 설계

In-Depth

🕹 기술 스택

🔓 기술 공유

🔥 트러블 슈팅


Ground Rule

📐 Common

🐱 Github


ETC

Clone this wiki locally