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

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

相关推荐
许思王1 分钟前
【Python】组合数据类型:序列,列表,元组,字典,集合
开发语言·人工智能·python
程序员的开发手册1 小时前
新手教学系列——慎用Flask-SQLAlchemy慢日志记录
数据库·python·flask·sqlalchemy
木觞清3 小时前
Django学习第三天
python·学习·django
电饭叔3 小时前
《python程序语言设计》2018版第5章第52题利用turtle绘制sin函数
开发语言·python
YCCX_XFF214 小时前
ImportError: DLL load failed while importing _imaging: 操作系统无法运行 %1
开发语言·python
FutureUniant5 小时前
GitHub每日最火火火项目(7.7)
python·计算机视觉·ai·github·视频
杰哥在此6 小时前
Java面试题:讨论持续集成/持续部署的重要性,并描述如何在项目中实施CI/CD流程
java·开发语言·python·面试·编程
PY1787 小时前
Python的上下文管理器
数据库·python·oracle
MasterSpring7 小时前
spring cloud gateway客户端websocket断开连接,服务侧连接没有关闭的问题处理
websocket·spring cloud·gateway
Struggle to dream7 小时前
Python编译器的选择
开发语言·python