且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

RestClientException:无法提取响应.找不到合适的HttpMessageConverter

更新时间:2023-11-22 11:57:40

此处的主要问题是从服务接收到的内容类型[text/html; charset = iso-8859-1] ,实际内容类型应为 application/json; charset = iso-8859-1

The main problem here is content type [text/html;charset=iso-8859-1] received from the service, however the real content type should be application/json;charset=iso-8859-1

为了克服这个问题,您可以引入自定义消息转换器.并为所有类型的响应注册它(即忽略响应内容类型标头).就是这样

In order to overcome this you can introduce custom message converter. and register it for all kind of responses (i.e. ignore the response content type header). Just like this

List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();        
//Add the Jackson Message converter
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();

// Note: here we are making this converter to process any kind of response, 
// not only application/*json, which is the default behaviour
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL));        
messageConverters.add(converter);  
restTemplate.setMessageConverters(messageConverters);