【问题标题】:Spring Android REST Template parse JSON data with Content Type text/htmlSpring Android REST 模板使用内容类型 text/html 解析 JSON 数据
【发布时间】:2014-03-11 14:53:57
【问题描述】:

我正在使用 android spring REST 模板从外部 API 中提取一些数据。 这些 API 返回 JSON 字符串,但响应内容类型为“text/html”,如果内容类型为“application/json”,我可以轻松解析数据而不会出现任何问题,因为此 API 是第 3 方 API,我无法更改内容类型的回应。

我正在使用 “映射杰克逊HttpMessageConverter” 类作为消息转换器。

当我尝试解析数据时遇到异常。

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type .... and content type [text/html;charset=utf-8]

有什么配置、参数之类的可以解析这些JSON数据吗?

【问题讨论】:

    标签: android jackson spring-android


    【解决方案1】:

    默认情况下,MappingJacksonHttpMessageConverter 仅支持 application/json 媒体类型。但是,您可以轻松地将其配置为支持其他媒体类型:

    MappingJacksonHttpMessageConverter converter = new MappingJacksonHttpMessageConverter();
    converter.setSupportedMediaTypes(Collections.singletonList(MediaType.TEXT_HTML));
    

    现在,当您收到响应时,RestTemplate 应该将MappingJacksonHttpMessageConverter 识别为能够解析它。

    【讨论】:

    • 太好了,感谢您的回答,这就是我一直在寻找的。 :)
    猜你喜欢
    • 2011-02-13
    • 1970-01-01
    • 2014-03-11
    • 2015-05-29
    • 2019-12-07
    • 1970-01-01
    • 1970-01-01
    • 2018-12-28
    • 2012-12-13
    相关资源
    最近更新 更多