1

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

我使用“MappingJacksonHttpMessageConverter”类作为消息转换器。

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

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 数据的东西?

4

1 回答 1

11

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

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

现在,当您收到响应时,RestTemplate应该确定MappingJacksonHttpMessageConverter能够解析它。

于 2014-03-12T15:43:43.837 回答