no suitable HttpMessageConverter found for response type 异常处理
程序员文章站
2022-04-15 18:31:50
报错: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();...
报错:
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);
}
本文地址:https://blog.csdn.net/qq_37539693/article/details/110222782