问题描述:
springboot项目使用feign调用第三方接口,第三方接口定义为post请求json格式,但是写代码时写成了@GetMapping,如下所示:
@GetMapping("/query")
JSONObject query(@RequestBody JSONObject req);
请求第三方接口是可以请求成功的,但是如果用postman用get方式请求是失败的。后期项目将feign底层http调用改为了okhttp之后该接口调用失败,排查后发现Feign 默认使用 JDK 的 HttpURLConnnection 来处理 HTTP URL 的请求,会将get请求转为post请求。
private synchronized OutputStream getOutputStream0() throws IOException {
try {
if (!this.doOutput) {
throw new ProtocolException("cannot write to a URLConnection if doOutput=false - call setDoOutput(true)");
} else {
if (this.method.equals("GET")) {
this.method = "POST";
}
if ("TRACE".equals(this.method) && "http".equals(this.url.getProtocol())) {
throw new ProtocolException("HTTP method TRACE doesn't support output");
} else if (this.inputStream != null) {
throw new ProtocolException("Cannot write output after reading input.");
} else {
if (!this.checkReuseConnection()) {
this.connect();
}
boolean var1 = false;
String var8 = this.requests.findValue("Expect");
if ("100-Continue".equalsIgnoreCase(var8) && this.streaming()) {
this.http.setIgnoreContinue(false);
var1 = true;
}
if (this.streaming() && this.strOutputStream == null) {
this.writeRequests();
}
if (var1) {
this.expect100Continue();
}
this.ps = (PrintStream)this.http.getOutputStream();
if (this.streaming()) {
if (this.strOutputStream == null) {
if (this.chunkLength != -1) {
this.strOutputStream = new HttpURLConnection.StreamingOutputStream(new ChunkedOutputStream(this.ps, this.chunkLength), -1L);
} else {
long var3 = 0L;
if (this.fixedContentLengthLong != -1L) {
var3 = this.fixedContentLengthLong;
} else if (this.fixedContentLength != -1) {
var3 = (long)this.fixedContentLength;
}
this.strOutputStream = new HttpURLConnection.StreamingOutputStream(this.ps, var3);
}
}
return this.strOutputStream;
} else {
if (this.poster == null) {
this.poster = new PosterOutputStream();
}
return this.poster;
}
}
}
} catch (RuntimeException var5) {
this.disconnectInternal();
throw var5;
} catch (ProtocolException var6) {
int var2 = this.responseCode;
this.disconnectInternal();
this.responseCode = var2;
throw var6;
} catch (IOException var7) {
this.disconnectInternal();
throw var7;
}
}
网友评论