no suitable HttpMessageConverter found for response type 异常
报错:
org.springframework.web.client.RestClientException:
Could not extract response: no suitable HttpMessageConverter found for response type [...]
and content type [text/html;charset=utf-8]
解决办法:
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "text/html;charset=utf-8");
HttpEntity requestEntity=new HttpEntity<>(headers);
RestTemplate restTemplate = getRestTemplate(timeOut);
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setSupportedMediaTypes(Arrays.asList(MediaType.ALL));
restTemplate.getMessageConverters().add(0, converter);
ResponseEntity baseResultVOResponseEntity = restTemplate.exchange(url,
HttpMethod.POST,
requestEntity,
//返回值类型
new ParameterizedTypeReference<ResponseDTO<SomeResponseDTO>>() {
});
}
private static RestTemplate getRestTemplate(Integer timeout) {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setConnectTimeout(timeout);
return new RestTemplate(requestFactory);
}