1. 问题原因
RestTemplate的构造器创建了一个默认字符集为ISO-8859-1的StringHttpMessageConverter对象, 它的默认编码格式为ISO-8859-1, 所以需要修改这个StringHttpMessageConverter对象的编码格式即可
2. 解决方案
@Bean
public RestTemplate getRestTemplate() {
SimpleClientHttpRequestFactory httpRequestFactory = new SimpleClientHttpRequestFactory();
RestTemplate restTemplate = new RestTemplate(httpRequestFactory);
List<HttpMessageConverter<?>> list = restTemplate.getMessageConverters();
for (HttpMessageConverter<?> httpMessageConverter : list) {
if(httpMessageConverter instanceof StringHttpMessageConverter) {
((StringHttpMessageConverter) httpMessageConverter).setDefaultCharset(StandardCharsets.UTF_8);
break;
}
}
return restTemplate;
}
标签:httpRequestFactory,httpMessageConverter,SpringBoot,StringHttpMessageConverter,Re
From: https://www.cnblogs.com/virgosnail/p/17828497.html