Search code examples
androidjacksonspring-android

Spring Android REST Template parse JSON data with Content Type text/html


I'm using android spring REST template to extract some data from external APIs. those APIs return JSON string but response content type is "text/html", If the content type is "application/json" I can easily parse data without any issue, since this APIs are 3rd party APIs I can't change the content type of the response.

I'm using "MappingJacksonHttpMessageConverter" class as the message converter.

I'm getting below exception when I try to parse data.

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

Is there any configuration,parameter or something where I can parse these JSON data?


Solution

  • By default the MappingJacksonHttpMessageConverter only supports the application/json media type. However, you can easily configure it to support other media types:

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

    Now when you receive the response, RestTemplate should identify MappingJacksonHttpMessageConverter as being able to parse it.