Spring Cloud 乱码

Spring Boot RestTemplate 中文乱码

解决方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.addAll(restTemplate.getMessageConverters());
for(HttpMessageConverter<?> converter :messageConverters) {
if(converter instanceof StringHttpMessageConverter) {
converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
break;
}
}
restTemplate.setMessageConverters(messageConverters);
return restTemplate;
}

Spring Cloud Feign HttpClient 中文乱码

Spring Cloud Feign HttpClient

1
2
3
4
5
6
7
8
9
10
11
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>

<dependency>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-httpclient</artifactId>
<version>8.6.0</version>
</dependency>

Spring Cloud Feign HttpClient 传递中文乱码

推荐使用 ok-http

1
2
3
4
5
6
7
8
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</dependency>