OpenFeign的get请求被强制转化成post请求

本文详细解析了Feign框架在使用GET请求时遇到的问题及解决方法,包括如何正确配置以支持GET请求,避免将GET请求错误地转换为POST请求,以及推荐的依赖和配置调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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。

参考这篇文章https://www.jianshu.com/p/a9a7978f7bb8?tdsourcetag=s_pcqq_aiomsg

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值