Chinese garbled problems occur after Spring RestTemplate calls other people's interfaces remotely

1. Reason

Since the default character set of the StringHttpMessageConverter initialized by the default construction method of RestTemplate is ISO-8859-1, the response content of the RestTemplate request will appear Chinese garbled.

 

2. Solve

When configuring @Bean in springboot, the StringHttpMessageConverter encoding should be changed to UTF8 format. as follows:

@Bean    
public RestTemplate restTemplate() {

    RestTemplate restTemplate = new RestTemplate();

    restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));        

    return restTemplate;

}

 

Guess you like

Origin blog.csdn.net/weixin_45450428/article/details/105126026