resttemplate getForObject map responsetype

后端 未结 6 1749
囚心锁ツ
囚心锁ツ 2021-02-06 23:58

Update 02/05/2018 (about 4 years later)...I tested this again as people have been upvoting my question/answer and Sotirios Delimanolis is correct that I should not h

6条回答
  •  不思量自难忘°
    2021-02-07 00:36

    As I had previously noted, your error message is showing us that you are receiving application/octet-stream as a Content-Type.

    org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [interface java.util.Map] and content type [application/octet-stream]
    

    As such, Jackson's MappingJackson2HttpMessageConverter cannot parse the content (it's expecting application/json).


    Original answer:

    Assuming your HTTP response's Content-Type is application/json and you have have Jackson 1 or 2 on the classpath, a RestTemplate can deserialize JSON like you have into a java.util.Map just fine.

    With the error you are getting, which you haven't shown in full, either you've registered custom HttpMessageConverter objects which overwrite the defaults ones, or you don't have Jackson on your classpath and the MappingJackson2HttpMessageConverter isn't registered (which would do the deserialization) or you aren't receiving application/json.

提交回复
热议问题