Feign原生不支持get请求。Feign默认使用的是POST请求,即使设定请求方式为GET也不会生效,如果需要使用GET的请求方式,需要额外加依赖openFeign。
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
问题:Feign使用get方式传参实体类会被强制转换成post方式,可能会报异常:
(get传单个参数,map以及post传实体类等都是可以的,只有get传实体类时会被转成post)
"Request method 'POST' not supported"
原因:Feign默认使用JDK的连接实现类URLConnection,在源码中:
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";
}
解决方法:
1.参数分开写:(@RequestParam String userId,@RequestParam String username),参数太多时不便于使用
2.传参使用map
3.加入Feign的配置项
feign:
httpclient:
enabled: true
加依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.9</version>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
<version>10.2.3</version>
</dependency>
但是可能存在参数传递不过去的问题,反正我是没试过。
4.增加注解 @SpringQueryMap替换@RequestBody
这个可能需要升级到高版本的feign。