您的位置:首页技术文章
文章详情页

spring cloud Feign使用@RequestLine遇到的坑

浏览:5日期:2023-07-07 08:30:43
Feign使用@RequestLine遇到的坑

如何在微服务项目中调用其它项目的接口试使用spring cloud feign声明式调用。

/** * 客户端请去 * @author RAY * */@FeignClient(name='store',configuration=FooConfiguration .class)public interface UserFeignClient { @RequestLine('GET /simple/{id}') public User findById(@Param('id') Long id);}

但是启动得时候报错:

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

官方文档说明

@RequestLine is a core Feign annotation, but you are using the Spring Cloud @FeignClientwhich uses Spring MVC annotations.

spring cloud Feign使用@RequestLine遇到的坑

意思就是feign 默认使用的是spring mvc 注解(就是RequestMapping 之类的) ,所以需要通过新增一个配置类来修改其“契约”。

@Configurationpublic class FooConfiguration { @Bean public Contract feignContract() {return new feign.Contract.Default();//使用feign自带契约 }}

PS : feignContract方法名不要跟一样。否则启动得时候会报错。 得改一个跟类名不一样得方法名!

@RequestLine的使用及配置

@RequestLine与其它请求不同,只需要简单写请求方式和路径就能达到请求其它服务的目的。

@FeignClient(value = 'feign-server',configuration = FeignConfig.class) //需要一个配置文件public interface TestService { @RequestLine('POST /feign/test') //对应请求方式和路径 String feign(@RequestBody UserDO userDO);}

@EnableFeignClients@SpringBootConfigurationpublic class FeignConfig { @Bean public Contract contract(){return new feign.Contract.Default(); }}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持好吧啦网。

标签: Spring
相关文章: