TIL)23.08.21(JSON parse 오류)

주민·2023년 8월 21일
0

TIL

목록 보기
66/84

문제

팀프로젝트에서 기본 create api 만들고 테스트를 했는데 아래와 같은 오류가 나왔다.

  • PostMan
  • IntelliJ

    Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of com.sparta.petnexus.postComment.dto.PostCommentRequestsDto (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)]

시도

Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of

위 내용으로 검색해보니 Body로 보낸 json 파일을 읽지 못하니 postman의 Content-Type:application/json 으로 수정 & 추가 하라는 것이었는데 이미 들어가 있다...

문제 해결 & 알게된 점

참고블로그
objectMapper에서 생성자의 유무를 판단하고 null일 때 기본생성자를 만들어 준다. 다만, 기본 생성자 생성이 생략되는 경우가 있는데 멤버 변수가 Builde 사용 중에 단일 맴베 변수를 사용하면 생략되는 것으로 추측된다.

* requestDto
@Getter
@Builder
public class PostCommentRequestsDto {
    private String comment;

    public PostComment toEntity(Post post, User user){
        return PostComment.builder()
                .comment(this.comment)
                .post(post)
                .user(user)
                .build();
    }
}

NoArgsConstructor(기본 생성자)와 AllArgsConstructor(Builder 오류 때문에 추가) 애너테이션을 추가하니 정상적으로 실행되었다.

0개의 댓글

관련 채용 정보