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请求乱码内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论