MappingJackson2HttpMessageConverter
默认只支持 application/json
格式,,如果服务器返回的是text/plain
不能转换
扩展一个可以转换text/plain
的转换器:
java
public class RestTemplateMessageConverter extends MappingJackson2HttpMessageConverter {
public RestTemplateMessageConverter() {
List<MediaType> mediaTypeList = new ArrayList<>();
mediaTypeList.add(MediaType.APPLICATION_JSON);
mediaTypeList.add(MediaType.TEXT_PLAIN);
setSupportedMediaTypes(mediaTypeList);
}
}
注入restTemplate
java
@Bean
public RestTemplate restTemplate(){
RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
messageConverters.add(new RestTemplateMessageConverter());
return restTemplate;
}