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号13 分钟前
【python】—conda新建python3.11的环境报错
python·conda·python3.11
ZHOU_WUYI1 小时前
Flask Docker Demo 项目指南
python·docker·flask
Chase_Mos5 小时前
Spring 必会之微服务篇(1)
java·spring·微服务
码上淘金6 小时前
【Python】Python常用控制结构详解:条件判断、遍历与循环控制
开发语言·python
Brilliant Nemo6 小时前
四、SpringMVC实战:构建高效表述层框架
开发语言·python
2301_787552876 小时前
console-chat-gpt开源程序是用于 AI Chat API 的 Python CLI
人工智能·python·gpt·开源·自动化
懵逼的小黑子6 小时前
Django 项目的 models 目录中,__init__.py 文件的作用
后端·python·django
Y3174297 小时前
Python Day23 学习
python·学习
Ai尚研修-贾莲7 小时前
Python语言在地球科学交叉领域中的应用——从数据可视化到常见数据分析方法的使用【实例操作】
python·信息可视化·数据分析·地球科学
撸码到无法自拔7 小时前
docker常见命令
java·spring cloud·docker·容器·eureka