当前位置: 代码网 > it编程>编程语言>Java > SpringBoot中restTemplate请求存在乱码问题的解决方法

SpringBoot中restTemplate请求存在乱码问题的解决方法

2024年11月22日 Java 我要评论
springboot中的resttemplate请求存在乱码问题的解决搜索网上各种解法,最后在不断的一点点对比中,排查到了问题,是resttemplate不支持gzip,对返回的数据不能对gzip自动

springboot中的resttemplate请求存在乱码问题的解决

搜索网上各种解法,最后在不断的一点点对比中,排查到了问题,是resttemplate不支持gzip,对返回的数据不能对gzip自动解压,因此需要去掉header中的accept-encoding

网上提到的另一种解决方式是配置

import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.apache.http.client.httpclient;
import org.apache.http.impl.client.closeablehttpclient;
import org.apache.http.impl.client.httpclients;
import org.apache.http.impl.conn.poolinghttpclientconnectionmanager;
import org.springframework.http.httpheaders;
import org.springframework.http.client.httpcomponentsclienthttprequestfactory;
import org.springframework.web.client.resttemplate;

@configuration
public class resttemplateconfig {

    @bean
    public resttemplate resttemplate() {
        // 创建 httpclient
        closeablehttpclient httpclient = httpclients.custom()
                .setconnectionmanager(new poolinghttpclientconnectionmanager())
                .build();

        // 配置 requestfactory
        httpcomponentsclienthttprequestfactory requestfactory = new httpcomponentsclienthttprequestfactory(httpclient);
        resttemplate resttemplate = new resttemplate(requestfactory);

        // 添加请求头,告知服务器支持 gzip
        resttemplate.getinterceptors().add((request, body, execution) -> {
            request.getheaders().add(httpheaders.accept_encoding, "gzip, deflate, br");
            return execution.execute(request, body);
        });
        return resttemplate;
    }
}

而实际运行时还是会报错的。
一个解决方案就是 headers.remove(httpheaders.accept_encoding),但是并不完美。

    @getmapping("/**")
    public responseentity<string> proxygetrequest(@requestheader httpheaders headers, httpservletrequest request) {
        string requesturi = request.getrequesturi().replace("/proxy", "");
        string targeturl = "https://pre-youku-prefect-v2.alibaba-inc.com" + requesturi;

        headers.remove(httpheaders.accept_encoding); //必须去掉gzip压缩
        responseentity<string> response = resttemplate.exchange(targeturl, httpmethod.get, new httpentity<>(headers), string.class);
        return responseentity.status(response.getstatuscode()).body(response.getbody());
    }

最终原因

再深入分析后,发现实现代理时,应返回字节流,而不能是字符流,就能完美的实现代理功能

@restcontroller
@requestmapping("/proxy")
public class proxycontroller {

    private final resttemplate resttemplate;

    public proxycontroller(resttemplate resttemplate) {
        this.resttemplate = resttemplate;
    }

    @getmapping("/**")
    public responseentity<byte[]> proxygetrequest(@requestheader httpheaders headers, httpservletrequest request) {
        string requesturi = request.getrequesturi().replace("/proxy", "");
        string targeturl = "https://xxx.com" + requesturi;
        responseentity<byte[]> response = resttemplate.exchange(targeturl, httpmethod.get, new httpentity<>(headers), byte[].class);
        return response;
    }

    @postmapping("/**")
    public responseentity<byte[]> proxypostrequest(@requestheader httpheaders headers, @requestbody string body, httpservletrequest request) {
        string requesturi = request.getrequesturi().replace("/proxy", "");
        string targeturl = "https://xxx.com" + requesturi;
        responseentity<byte[]> response = resttemplate.exchange(targeturl, httpmethod.post, new httpentity<>(body, headers), byte[].class);
        return response;
    }
}

到此这篇关于springboot中resttemplate请求存在乱码问题的解决方法的文章就介绍到这了,更多相关springboot resttemplate请求乱码内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com