'jackson deserialize'에 해당되는 글 1건

  1. 2019.04.11 [2019-04-11] Spring RestTemplate response deserialize 중 HttpMessageNotReadableException

외부 API를 Spring RestTemplate로 호출하는데

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: 

Can not instantiate value of type java.util.HashMap from String value ('fail'); no single-String constructor/factory method

위의 Exception이 발생.

restTemplate.exchange에 세팅한 responseType class는 아래의 

TestResponse.java

API 호출 class

ApiTest.java

외부 API에서 응답 데이터를

위 처럼 넘겨줄 때는 정상적으로 변환되어 TestResponse의 HashMap<String, Object> result에 세팅되나,

아래와 같이 response가 오게되면

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: 

Can not instantiate value of type java.util.HashMap from String value ('fail'); no single-String constructor/factory method

위 에러 발생(HashMap에 "fail" 이라는 String value 값을 세팅할 수 없다(?))

외부 API를 수정할 수 없어(?) 서버쪽에서 변환 시 예외 처리를 하도록 함.

CustomDeserializer.java

생성한 CustomDeserializer를 TestResponse에 설정

TestResponse.java

이제 {"result":"success"} 형태도 받을 수 있음.

result.get("msg").toString() == "success"  

Posted by 스트라
,