SpringBoot/오류

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.lang.String` from Array value (token `JsonToken.START_ARRAY`)

se0nghyun2 2023. 1. 3. 21:53

배경

카카오 책검색 API 응답 도중 아래와 같은 에러가 발생했다. 

더보기

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.lang.String` from Array value (token `JsonToken.START_ARRAY`)

 

-   카카오 API를 통해 아래와 같은 구조의 json 응답

{
  "documents": [
    {
      "authors": [ "이보영" ],
      "contents": "content1",
      "title": "뜯어먹는 중학 영숙어 1000(스프링)(스프링)"
    },
    {
      "authors": [ "정혜련", "진효정" ],
      "contents": "content3"
      "title": "공무원 국어 매일 기출한자(빈출순)(2020)(에듀윌)(스프링)"
    }
}

 

- DTO 및 서비스코드

@NoArgsConstructor
@Getter
@Setter
@ToString
public class searchedDTOKAKAOList {
    String authors; //에러 발생 원인
    String contents;
    String datetime;
    String isbn;
    String price;
    String publisher;
    String sale_price;
    String status;
    String thumbnail;
    String title;
}



//서비스 코드 내 
HttpResponse httpResponse = httpClient.execute(getRequest);
HttpEntity httpEntity = httpResponse.getEntity();

content= EntityUtils.toString(httpEntity);

searchedDTOKAKAO= objectMapper.readValue(content,SearchedDTOKAKAO.class); //문제 발생(authors 매핑 불가)

 

매핑되지 않는 authors 프로퍼티와 authors field 형 때문에 발생!!

 


해결

@NoArgsConstructor
@Getter
@Setter
@ToString
public class searchedDTOKAKAOList {
    List<String> authors; //수정
    String contents;
    String datetime;
    String isbn;
    String price;
    String publisher;
    String sale_price;
    String status;
    String thumbnail;
    String title;
}