解決FeignClient發(fā)送post請求異常的問題_第1頁
解決FeignClient發(fā)送post請求異常的問題_第2頁
解決FeignClient發(fā)送post請求異常的問題_第3頁
解決FeignClient發(fā)送post請求異常的問題_第4頁
解決FeignClient發(fā)送post請求異常的問題_第5頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

第解決FeignClient發(fā)送post請求異常的問題通過對FeignClient的源碼閱讀,發(fā)現(xiàn)問題不是出在參數(shù)解析上,而是在使用ApacheHttpClient進行請求時,其將查詢參數(shù)放進請求body中了,下面看源碼具體是如何處理的

feign.httpclient.ApacheHttpClient這是feign-httpclient進行實際請求的方法

@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ù)取出來封裝成了HttpEntity

//就是在這里查詢參數(shù)被丟棄了,準確的說是被轉(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)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論