RestTemplate中文乱码问题
程序员文章站
2022-06-26 23:29:47
...
package com.ebtrust.configuration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import java.nio.charset.StandardCharsets;
import java.util.List;
/**
* 功能描述: RestTemplate配置
* @author luxiaomeng
* @date 2021/6/24 11:21
* @修改日志:
*/
@Configuration
public class RestTemplateConfiguration {
/**
* 功能描述: 配置RestTemplate中文乱码问题
* @author luxiaomeng
* @date 2021/6/24 11:22
* @修改日志:
*/
@Bean
// @ConditionalOnMissingBean(RestTemplate.class)
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
for (int i = 0; i < messageConverters.size(); i++) {
HttpMessageConverter<?> httpMessageConverter = messageConverters.get(i);
if (httpMessageConverter.getClass().equals(StringHttpMessageConverter.class)) {
messageConverters.set(i, new StringHttpMessageConverter(StandardCharsets.UTF_8));
}
}
return restTemplate;
}
}
上一篇: CSS 预处理语言之 Scss 篇
下一篇: vue-cli3.0+安装使用scss