spring cloud Feign使用@RequestLine遇到的坑
如何在微服務(wù)項(xiàng)目中調(diào)用其它項(xiàng)目的接口試使用spring cloud feign聲明式調(diào)用。
/** * 客戶端請去 * @author RAY * */@FeignClient(name='store',configuration=FooConfiguration .class)public interface UserFeignClient { @RequestLine('GET /simple/{id}') public User findById(@Param('id') Long id);}
但是啟動得時(shí)候報(bào)錯(cuò):
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.
意思就是feign 默認(rèn)使用的是spring mvc 注解(就是RequestMapping 之類的) ,所以需要通過新增一個(gè)配置類來修改其“契約”。
@Configurationpublic class FooConfiguration { @Bean public Contract feignContract() {return new feign.Contract.Default();//使用feign自帶契約 }}
PS : feignContract方法名不要跟一樣。否則啟動得時(shí)候會報(bào)錯(cuò)。 得改一個(gè)跟類名不一樣得方法名!
@RequestLine的使用及配置@RequestLine與其它請求不同,只需要簡單寫請求方式和路徑就能達(dá)到請求其它服務(wù)的目的。
@FeignClient(value = 'feign-server',configuration = FeignConfig.class) //需要一個(gè)配置文件public interface TestService { @RequestLine('POST /feign/test') //對應(yīng)請求方式和路徑 String feign(@RequestBody UserDO userDO);}
@EnableFeignClients@SpringBootConfigurationpublic class FeignConfig { @Bean public Contract contract(){return new feign.Contract.Default(); }}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. CSS hack用法案例詳解2. 詳解瀏覽器的緩存機(jī)制3. ASP腳本組件實(shí)現(xiàn)服務(wù)器重啟4. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案5. AspNetCore&MassTransit Courier實(shí)現(xiàn)分布式事務(wù)的詳細(xì)過程6. ASP動態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗(yàn)分享7. 利用CSS制作3D動畫8. .NET使用YARP通過編碼方式配置域名轉(zhuǎn)發(fā)實(shí)現(xiàn)反向代理9. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)財(cái)務(wù)記賬管理系統(tǒng)10. XML入門精解之結(jié)構(gòu)與語法
