I have simple java spring method for creating object
RestTemplate restTemplate = new RestTemplate();
Address address = restTemplate.getForObject(url, Address.class);
But the server responds me JSON string with wrong Content-Type: text/plain instead of application/json (checked in Postman). And I get the exception:
Could not extract response: no suitable HttpMessageConverter found for response type [class Address] and content type [text/plain;charset=utf-8]
So I think, I need change response header Content-Type to right application/json, that MappingJackson2HttpMessageConverter find out JSON string and run code as well.
After trying for an hour, I found a short and easy way.
Json converter by default supports only "application/json". We just override it to support "text/plain".
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
// support "text/plain"
converter.setSupportedMediaTypes(Arrays.asList(TEXT_PLAIN, APPLICATION_JSON));
RestTemplate template = new RestTemplate();
template.getMessageConverters().add(converter);
// It's ok now
MyResult result = tmp.postForObject("http://url:8080/api",
new MyRequest("param value"), MyResult.class);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With