SpringCloud Feign 报错 Request method ‘POST‘ not supported 的解决办法

通过SpringCloud Feign 调用其他项目或微服务的接口方法时报Request method 'POST' not supported 的错误:

问题出现原因:

  1. 可能的原因是 Feign 默认使用的请求方法为 POST,而不是 GET。
  2. 另外也有可能是由于消费方法和接口方法使用的注解不正确;如果 Feign 代理的是 get 请求,则每个参数必须带上 @RequestParam 注解,否则会报 POST not supported
  3. 可能是SpringCloud原生的Feign,可以考虑使用OpenFeign
  4. 可以考虑修改使用feign默认配置的方法默认使用post
  5. 通过feign调用get请求时,如果接口参数含有POJO时,可能会出现 'POST' not supported

解决方法

  1. 在Feign代理接口方法处使用 @RequestMapping 替代 @GetMapping: 尝试使用 @RequestMapping 注解代替 @GetMapping 注解,显式指定请求方法为 GET。示例代码如下:

    java 复制代码
    @FeignClient(name = "your-service-name")
    public interface YourFeignClient {
    
        @RequestMapping(method = RequestMethod.GET, value = "/your/api/path")
        String yourGetMethod();
    }
  2. 另外也有可能是由于消费方法和接口方法使用的注解不正确;

    @RequestBody-------------> @PostMapping

    @RequestParam 、@PathVariable------@GetMapping

    java 复制代码
     @GetMapping("/user/list")
     List<userInfo> list(@PathVariable List<String> userIdList);
  3. 在pom.xml文件引入openfeign

    java 复制代码
    	<!-- 在 pom 中添加依賴-->
    	     <dependency>
    	            <groupId>io.github.openfeign</groupId>
    	            <artifactId>feign-httpclient</artifactId>
    	     </dependency>
  4. 自定义 FeignClient 的配置: 在 FeignClient 的配置类中,可以自定义 Feign 的配置,指定 FeignClient 默认的请求方法为 GET。示例代码如下:

    java 复制代码
    @Configuration
    public class FeignConfiguration {
    
        @Bean
        public RequestInterceptor requestInterceptor() {
            return new RequestInterceptor() {
                @Override
                public void apply(RequestTemplate template) {
                    template.method("GET");
                }
            };
        }
    }
  5. 还有一种情况,涉及到实体类的情况,如果接口参数含有POJO,feign调用入参是POJO的GET请求时,会往body里设置参数,而因为发现body里有数据,就会自动将get请求转为post,feign版本在2.0以上,在消费方法的实体类对象参数前新增@SpringQueryMap注解即可

    java 复制代码
    @FeignClient("user")
    public class userList{
    	@GetMapping(path="/your/api/path")
    	String queryUser(@SpringQueryMap User user);
    }

总结:以上就是总结起来的可能导致问题以及他们的解决办法~~,如果有效解决的亲的问题,请点赞加关注哦,么么哒~~

相关推荐
伊布拉西莫16 小时前
Spring 6.x HTTP interface 使用说明
spring·restclient
蛋仔聊测试16 小时前
Playwright 中route 方法模拟测试数据(Mocking)详解
前端·python·测试
今天没有盐16 小时前
Pandas缺失值处理完全指南:从基础操作到高级技巧
python·pycharm·编程语言
程序员小远16 小时前
快速定位bug,编写测试用例
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·bug
B站_计算机毕业设计之家16 小时前
大数据YOLOv8无人机目标检测跟踪识别系统 深度学习 PySide界面设计 大数据 ✅
大数据·python·深度学习·信息可视化·数据挖掘·数据分析·flask
老歌老听老掉牙16 小时前
解决 PyQt5 中 sipPyTypeDict() 弃用警告的完整指南
python·qt
武陵悭臾16 小时前
Python应用开发学习: Pygame 中实现数字水平靠右对齐和垂直靠底对齐
python·学习·程序人生·游戏·个人开发·学习方法·pygame
兜有米啦17 小时前
python练习题3
开发语言·python
你才是向阳花17 小时前
如何用Python实现飞机大战小游戏
开发语言·python·pygame
草莓熊Lotso17 小时前
C++ 方向 Web 自动化测试实战:以博客系统为例,从用例到报告全流程解析
前端·网络·c++·人工智能·后端·python·功能测试