restTemplate中文乱码问题

该代码段展示了如何使用Spring的@Value注解从配置中读取远程服务的最大连接总数、每个路由的最大连接数、连接超时和读取超时。接着,它创建了一个RestTemplate实例,并定制了其HttpMessageConverter,确保StringHttpMessageConverter使用UTF-8编码并添加了FastJsonHttpMessageConverter来支持FastJson序列化。
摘要由CSDN通过智能技术生成

    @Value("${remote.maxTotalConnect:400}")
    private int maxTotalConnect;
    /* 单个主机的最大连接数 */
    @Value("${remote.maxConnectPerRoute:200}")
    private int maxConnectPerRoute;
    /* 连接超时默认2s */
    @Value("${remote.connectTimeout:10000}")
    private int connectTimeout;
    /* 读取超时默认30s */
    @Value("${remote.readTimeout:180000}")
    private int readTimeout;

 @Bean
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate(this.createFactory());
        List<HttpMessageConverter<?>> converterList = restTemplate.getMessageConverters();
        // 重新设置StringHttpMessageConverter字符集为UTF-8,解决中文乱码问题
        HttpMessageConverter<?> converterTarget = null;
        for (HttpMessageConverter<?> item : converterList) {
            if (StringHttpMessageConverter.class == item.getClass()) {
                converterTarget = item;
                break;
            }
        }
        if (null != converterTarget) {
            converterList.remove(converterTarget);
        }
        converterList.add(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        //加入FastJson转换器
        converterList.add(new FastJsonHttpMessageConverter());
        return restTemplate;
    }

    private ClientHttpRequestFactory createFactory() {
        if (this.maxTotalConnect <= 0) {
            SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
            factory.setConnectTimeout(this.connectTimeout);
            factory.setReadTimeout(this.readTimeout);
            return factory;
        }
        HttpClient httpClient = HttpClientBuilder.create().setMaxConnTotal(this.maxTotalConnect)
                .setMaxConnPerRoute(this.maxConnectPerRoute).build();
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(
                httpClient);
        factory.setConnectTimeout(this.connectTimeout);
        factory.setReadTimeout(this.readTimeout);
        return factory;
    }
RestTemplate在发送请求后返回的中文乱码问题可以通过编码转换来解决。根据引用\[2\]中的代码追踪,可以使用以下代码将返回的中文从ISO_8859_1编码转换为UTF-8编码: ```java String result = new String(response.getBody().getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8); ``` 另外,根据引用\[3\]中的分析,可以通过修改RestTemplate的默认编码格式来解决乱码问题。可以使用以下代码来设置RestTemplate的编码格式为UTF-8: ```java RestTemplate restTemplate = new RestTemplate(); List<HttpMessageConverter<?>> httpMessageConverters = restTemplate.getMessageConverters(); httpMessageConverters.stream().forEach(httpMessageConverter -> { if(httpMessageConverter instanceof StringHttpMessageConverter){ StringHttpMessageConverter messageConverter = (StringHttpMessageConverter) httpMessageConverter; messageConverter.setDefaultCharset(Charset.forName("UTF-8")); } }); String result = restTemplate.getForEntity(url, String.class).getBody(); ``` 通过以上两种方法,你可以解决RestTemplate返回中文乱码问题。 #### 引用[.reference_title] - *1* [RestTemplate 返回值中文乱码](https://blog.csdn.net/qq_34819372/article/details/117922798)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [restTemplate返回字符乱码问题](https://blog.csdn.net/haohaounique/article/details/116034779)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [RestTemplate返回值乱码解决](https://blog.csdn.net/myhAini/article/details/103276726)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值