分享

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

 vnxy001 2021-12-22

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>

搞定!

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多