feign客户端使用PostMapping和GetMapping报方法错误的问题

下午在写feign接口的时候老是遇到下面这个错误,但是我使用GetMapping和PostMapping注解很明显是指定了http方法的。

 Method getConversationById not annotated with HTTP method type (ex. GET, POST)

/**
 * 功能描述  获取一个对话信息
 *
 * @param convId 对话ID
 * @return Conversation
 * @throws
 * @author Wuli
 * @date 2019/4/16
 **/
@GetMapping("/{convId}")
public ConversationInfo getConversationById(@PathVariable("convId") String convId);

但是使用如下写法就可以运行起来


    @RequestLine("/GET {convId}")
    public ConversationInfo getConversationById(@PathVariable("convId") String convId);

本来是想全部用RequestLine注解来写的,但是发现这个接口的坑更多,比如方法超过两个接收参数经常会报Method has too many Body parameters 错误。

于是决定好好研究这个问题。

最终发现在RuleConfig中注入的Feign的接口,于是feignClient接口中只要是使用SpringMVC的注解都会报错。

 

解决办法如下:

1、注释掉上图的转发规则,feignClient中全部使用SpringMVC的注解

2、全部使用Feign的专用注解,Feign坑这么多,我决定还是不用feign的注解了,免得哪天又要爬坑  - -

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Feign客户端是一个轻量级的HTTP客户端,它使用注解方式定义API接口,并且能够与Spring Cloud等微服务框架无缝集成。下面是实现Feign客户端的步骤: 1. 添加依赖 在Maven或Gradle中添加Feign客户端的依赖。 Maven: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` Gradle: ```groovy implementation 'org.springframework.cloud:spring-cloud-starter-openfeign' ``` 2. 定义API接口 使用Feign注解定义调用的API接口,例如: ```java @FeignClient(name = "example-service") public interface ExampleServiceClient { @GetMapping("/example") String getExample(); @PostMapping("/example") void postExample(@RequestBody Example example); } ``` 3. 注入Feign客户端 在需要调用API的代码中注入Feign客户端,例如: ```java @Service public class ExampleService { private final ExampleServiceClient exampleServiceClient; public ExampleService(ExampleServiceClient exampleServiceClient) { this.exampleServiceClient = exampleServiceClient; } public String getExample() { return exampleServiceClient.getExample(); } public void postExample(Example example) { exampleServiceClient.postExample(example); } } ``` 4. 配置Feign客户端 可以在配置文件中配置Feign客户端的一些属性,例如: ```yaml example-service: url: http://example-service connectTimeout: 5000 readTimeout: 5000 ``` 以上就是实现Feign客户端的基本步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值