Error about ObjectMapper [JSON -> ValueObject] :: MismatchedInputException: Cannot deserialize value of type `java.lang.String` from Object value

Denia·2022년 10월 7일
0

TroubleShooting

목록 보기
1/24

ObjectMapper 를 사용하여 JSON 데이터를 VO[ValueObject]를 만들려고 하다가 에러가 발생했다.

다음의 JSON 데이터를 다음과 같은 VO 로 만들려고 했는데
다음과 같은 에러가 발생함

MismatchedInputException: Cannot deserialize value of type java.lang.String from Object value

//JSON

{
   "args":{},
   "headers":{
      "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
      "Accept-Encoding":"gzip, deflate, br",
      "Accept-Language":"ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7"
   },
   "origin":"121.150.166.133",
   "url":"https://httpbin.org/get"
}

===============================================================================

//VO

@JsonIgnoreProperties(ignoreUnknown = true)
class VO{
    private String args;
    private String origin;
    private String headers;
    private String url;

    public String getHeaders() {
        return headers;
    }

    public String getArgs() {
        return args;
    }

    public String getOrigin() {
        return origin;
    }

    public String getUrl() {
        return url;
    }

    @Override
    public String toString() {
        return String.format("args = %s, headers = %s ,origin = %s, url = %s", this.args, this.headers, this.origin, this.url);
    }

}

에러가 발생한 원인

JSON을 잘 살펴보면 "args" 와 "headers" 의 값은 단순 String이 아니라 { } 괄호가 쳐져있는 ★ 즉 Object 형태이다. ★

그러므로 Object를 String으로 만들 수 없는 것은 당연하므로 에러가 발생함.

처음에는 에러 발견을 못하다가 나와 비슷한 문제를 겪으신 분 덕분에 문제를 해결했다.

참고 블로그 : https://cocoon1787.tistory.com/826

profile
HW -> FW -> Web

0개의 댓글