SpringBoot中restTemplate請求存在亂碼問題的解決方法
SpringBoot中的restTemplate請求存在亂碼問題的解決

搜索網(wǎng)上各種解法,最后在不斷的一點(diǎn)點(diǎn)對比中,排查到了問題,是restTemplate不支持gzip,對返回的數(shù)據(jù)不能對gzip自動解壓,因此需要去掉header中的accept-encoding

網(wǎng)上提到的另一種解決方式是配置
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() {
// 創(chuàng)建 HttpClient
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(new PoolingHttpClientConnectionManager())
.build();
// 配置 RequestFactory
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
RestTemplate restTemplate = new RestTemplate(requestFactory);
// 添加請求頭,告知服務(wù)器支持 Gzip
restTemplate.getInterceptors().add((request, body, execution) -> {
request.getHeaders().add(HttpHeaders.ACCEPT_ENCODING, "gzip, deflate, br");
return execution.execute(request, body);
});
return restTemplate;
}
}
而實(shí)際運(yùn)行時還是會報(bào)錯的。
一個解決方案就是 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());
}
最終原因
再深入分析后,發(fā)現(xiàn)實(shí)現(xiàn)代理時,應(yīng)返回字節(jié)流,而不能是字符流,就能完美的實(shí)現(xiàn)代理功能
@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;
}
}
到此這篇關(guān)于SpringBoot中restTemplate請求存在亂碼問題的解決方法的文章就介紹到這了,更多相關(guān)SpringBoot restTemplate請求亂碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java編譯后的文件出現(xiàn)xx$1.class的原因及解決方式
這篇文章主要介紹了java編譯后的文件出現(xiàn)xx$1.class的原因及解決方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
idea中怎樣創(chuàng)建并運(yùn)行第一個java程序
這篇文章主要介紹了idea中怎樣創(chuàng)建并運(yùn)行第一個java程序問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
Java使用文本塊(Text Blocks)處理多行字符串的操作方法
本文介紹了Java文本塊(TextBlocks)語法,它簡化多行字符串的處理,保留文本格式、減少錯誤、提升代碼可讀性,本文詳細(xì)解釋了文本塊的語法、縮進(jìn)規(guī)則、常用轉(zhuǎn)義符及常見使用場景,同時注意事項(xiàng),需要的朋友可以參考下2026-05-05
Java使用設(shè)計(jì)模式中的工廠方法模式實(shí)例解析
當(dāng)系統(tǒng)準(zhǔn)備為用戶提供某個類的子類的實(shí)例,又不想讓用戶代碼和該子類形成耦合時,就可以使用工廠方法模式來設(shè)計(jì)系統(tǒng).工廠方法模式的關(guān)鍵是在一個接口或抽象類中定義一個抽象方法,下面我們會具體介紹Java使用設(shè)計(jì)模式中的工廠方法模式實(shí)例解析.2016-05-05

