【SpringCloud】Feign解决Get请求自动转化成POST的问题

Feign

/**
 * @author mirror
 */
@FeignClient(
        value = "pocket-account-management",
        configuration = FeignAccMgrServiceConfiguration.class)
public interface AccMgrService {
    ...
    @GetMapping(ACC_URL_PREFIX + "/query/currencys")
    ResultData queryCurrencysByIdx(@RequestBody QueryIdx queryIdx);
    ...
}

服务提供者

    @GetMapping("/query/currencys")
    public ResultData queryCurrencysByIdx(@RequestBody QueryIdx queryIdx) {
        ...
        return ...;
    }

调用时出现错误:Caused by: feign.FeignException$MethodNotAllowed: status 405 reading

明明Feign发送的是Get请求,到了提供者这边却变成了Post

原因

因为Feign默认使用的连接工具实现类,所以里面发现只要你有body体对象,就会强制的把GET请求转换成POST请求。

解决办法

步骤

  • 加入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>

搞定!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容