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

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

相关推荐
子燕若水2 小时前
uv 安装包
开发语言·chrome·python
weixin_307779132 小时前
Python获取能唯一确定一棵给定的树的最少数量的拓扑序列
数据结构·python
A.sir啊3 小时前
爬虫基础(六)代理简述
爬虫·python·网络协议
weixin_307779133 小时前
PySPARK带多组参数和标签的SparkSQL批量数据导出到S3的程序
大数据·数据仓库·python·sql·spark
Hi Man4 小时前
Python之如何在Visual Studio Code 中写的python程序打包成可以在Windows系统下运行的.exe程序
开发语言·vscode·python
Return-Log4 小时前
Matplotlab显示OpenCV读取到的图像
python·opencv
程序趣谈4 小时前
算法随笔_36: 复写零
数据结构·python·算法
九亿AI算法优化工作室&5 小时前
GWO优化LSBooST回归预测matlab
人工智能·python·算法·机器学习·matlab·数据挖掘·回归
武昌库里写JAVA5 小时前
redis原理之数据结构
spring boot·spring·毕业设计·layui·课程设计
平凡君5 小时前
缓存的今生今世
java·spring·缓存