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);
    }

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

相关推荐
霍志杰8 分钟前
记一次csv和xlsx之间的转换处理
python
测试199823 分钟前
Jmeter是如何实现接口关联的?
自动化测试·软件测试·python·测试工具·jmeter·职场和发展·接口测试
小蕾Java35 分钟前
PyCharm 2025:最新使用图文教程!
ide·python·pycharm
LB211240 分钟前
苍穹外卖-缓存套餐 Spring Cache day07
java·spring boot·spring
W.Buffer1 小时前
SpringCloud-Sentinel实战与源码分析:从流量防护到底层实现
spring·spring cloud·sentinel
java1234_小锋1 小时前
TensorFlow2 Python深度学习 - 卷积神经网络(CNN)介绍
python·深度学习·tensorflow·tensorflow2
java1234_小锋1 小时前
TensorFlow2 Python深度学习 - 循环神经网络(RNN)- 简介
python·深度学习·tensorflow·tensorflow2
大飞记Python1 小时前
Chromedriver放项目里就行!Selenium 3 和 4 指定路径方法对比 + 兼容写法
开发语言·python
小薛引路1 小时前
office便捷办公06:根据相似度去掉excel中的重复行
windows·python·excel
Hs_QY_FX1 小时前
Python 分类模型评估:从理论到实战(以信用卡欺诈检测为例)
人工智能·python·机器学习·数据挖掘·多分类评估