下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
第解決FeignClient發(fā)送post請求異常的問題通過對FeignClient的源碼閱讀,發(fā)現(xiàn)問題不是出在參數(shù)解析上,而是在使用ApacheHttpClient進(jìn)行請求時(shí),其將查詢參數(shù)放進(jìn)請求body中了,下面看源碼具體是如何處理的
feign.httpclient.ApacheHttpClient這是feign-httpclient進(jìn)行實(shí)際請求的方法
@Override
publicResponseexecute(Requestrequest,Request.Optionsoptions)throwsIOException{
HttpUriRequesthttpUriRequest;
try{
httpUriRequest=toHttpUriRequest(request,options);
}catch(URISyntaxExceptione){
thrownewIOException("URL'"+request.url()+"'couldn'tbeparsedintoaURI",e);
HttpResponsehttpResponse=client.execute(httpUriRequest);
returntoFeignResponse(httpResponse);
HttpUriRequesttoHttpUriRequest(Requestrequest,Request.Optionsoptions)throws
UnsupportedEncodingException,MalformedURLException,URISyntaxException{
RequestBuilderrequestBuilder=RequestBuilder.create(request.method());
//perrequesttimeouts
RequestConfigrequestConfig=RequestConfig
.custom()
.setConnectTimeout(options.connectTimeoutMillis())
.setSocketTimeout(options.readTimeoutMillis())
.build();
requestBuilder.setConfig(requestConfig);
URIuri=newURIBuilder(request.url()).build();
requestBuilder.setUri(uri.getScheme()+"://"+uri.getAuthority()+uri.getRawPath());
//requestqueryparams
ListNameValuePairqueryParams=URLEncodedUtils.parse(uri,requestBuilder.getCharset().name());
for(NameValuePairqueryParam:queryParams){
requestBuilder.addParameter(queryParam);
//requestheaders
booleanhasAcceptHeader=false;
for(Map.EntryString,CollectionStringheaderEntry:request.headers().entrySet()){
StringheaderName=headerEntry.getKey();
if(headerName.equalsIgnoreCase(ACCEPT_HEADER_NAME)){
hasAcceptHeader=true;
if(headerName.equalsIgnoreCase(Util.CONTENT_LENGTH)){
//The'Content-Length'headerisalwayssetbytheApacheclientandit
//doesn'tlikeustosetitaswell.
continue;
for(StringheaderValue:headerEntry.getValue()){
requestBuilder.addHeader(headerName,headerValue);
//someserverschokeonthedefaultacceptstring,sowe'llsetittoanything
if(!hasAcceptHeader){
requestBuilder.addHeader(ACCEPT_HEADER_NAME,"*/*");
//requestbody
if(request.body()!=null){
//body為空,則HttpEntity為空
HttpEntityentity=null;
if(request.charset()!=null){
ContentTypecontentType=getContentType(request);
Stringcontent=newString(request.body(),request.charset());
entity=newStringEntity(content,contentType);
}else{
entity=newByteArrayEntity(request.body());
requestBuilder.setEntity(entity);
//調(diào)用org.apache.http.client.methods.RequestBuilder#build方法
returnrequestBuilder.build();
}
org.apache.http.client.methods.RequestBuilder此類是HttpUriRequest的Builder類,下面看build方法
publicHttpUriRequestbuild(){
finalHttpRequestBaseresult;
URIuriNotNull=this.uri!=nullthis.uri:URI.create("/");
HttpEntityentityCopy=this.entity;
if(parameters!=null!parameters.isEmpty()){
//這里:如果HttpEntity為空,并且為POST請求或者為PUT請求時(shí),這個(gè)方法會(huì)將查詢參數(shù)取出來封裝成了HttpEntity
//就是在這里查詢參數(shù)被丟棄了,準(zhǔn)確的說是被轉(zhuǎn)換位置了
if(entityCopy==null(HttpPost.METHOD_NAME.equalsIgnoreCase(method)
||HttpPut.METHOD_NAME.equalsIgnoreCase(method))){
entityCopy=newUrlEncodedFormEntity(parameters,charset!=nullcharset:HTTP.DEF_CONTENT_CHARSET);
}else{
try{
uriNotNull=newURIBuilder(uriNotNull)
.setCharset(this.charset)
.addParameters(parameters)
.build();
}catch(finalURISyntaxExceptionex){
//shouldneverhappen
if(entityCopy==null){
result=newInternalRequest(method);
}else{
finalInternalEntityEclosingRequestrequest=newInternalEntityEclosingRequest(method);
request.setEntity(entityCopy);
result=request;
result.setProtocolVersion(this.version);
result.setURI(uriNotNull);
if(this.headergroup!=null){
result.setHeaders(this.headergroup.getAllHeaders());
result.setConfig(this.config);
retu
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- GB/T 28504.4-2025摻稀土光纖第4部分:摻鉺光纖特性
- 2025年大學(xué)第四學(xué)年(動(dòng)物醫(yī)學(xué))動(dòng)物醫(yī)學(xué)綜合實(shí)訓(xùn)試題及答案
- 2025年高職(工業(yè)機(jī)器人技術(shù))故障排查階段測試題及答案
- 2026年水利施工(排水管道施工)試題及答案
- 2025年高職(應(yīng)用化工技術(shù))應(yīng)用化工階段測試試題及答案
- 2025年高職物聯(lián)網(wǎng)工程(傳感器應(yīng)用)試題及答案
- 2025年高職水環(huán)境監(jiān)測與治理(水環(huán)境監(jiān)測)試題及答案
- 2025年大學(xué)二年級(土木工程)結(jié)構(gòu)力學(xué)基礎(chǔ)試題及答案
- 2025年中職(模具制造技術(shù))模具零件加工專項(xiàng)測試試題及答案
- 2025年中職(酒店管理)酒店安全實(shí)訓(xùn)階段測試題及答案
- 2026年煤礦礦長證考試題庫及答案
- 危重病人營養(yǎng)支持教案
- 《毛澤東思想概論》與《中國特色社會(huì)主義理論體系概論》核心知識點(diǎn)梳理及100個(gè)自測題(含答案)
- 分級護(hù)理質(zhì)量考核標(biāo)準(zhǔn)
- 電梯安裝文明施工方案
- 天津市專升本高等數(shù)學(xué)歷年真題(2016-2025)
- DB23∕T 3314-2022 黑龍江省土壤污染防治風(fēng)險(xiǎn)篩選指導(dǎo)值(試行)
- 2025年福建省年省直遴選筆試真題及答案
- 腳手架安全培訓(xùn)
- 2025年檢驗(yàn)檢測機(jī)構(gòu)內(nèi)部質(zhì)量控制標(biāo)準(zhǔn)模擬考試試題試卷
- 2025云南昆明元朔建設(shè)發(fā)展有限公司第一批收費(fèi)員招聘20人考試參考試題及答案解析
評論
0/150
提交評論