這篇文章主要介紹Spring Cloud怎么使用Feign構(gòu)造多參數(shù)的請(qǐng)求,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

GET請(qǐng)求多參數(shù)的URL
假設(shè)我們請(qǐng)求的URL包含多個(gè)參數(shù),例如http://microservice-provider-user/get?id=1&username=張三 ,要如何構(gòu)造呢?
我們知道,Spring Cloud為Feign添加了Spring MVC的注解支持,那么我們不妨按照Spring MVC的寫法嘗試一下:
@FeignClient("microservice-provider-user")
public interface UserFeignClient {
 @RequestMapping(value = "/get", method = RequestMethod.GET)
 public User get0(User user);
}然而,這種寫法并不正確,控制臺(tái)會(huì)輸出類似如下的異常。
feign.FeignException: status 405 reading UserFeignClient#get0(User); content:
{"timestamp":1482676142940,"status":405,"error":"Method Not Allowed","exception":"org.springframework.web.HttpRequestMethodNotSupportedException","message":"Request method 'POST' not supported","path":"/get"}
由異常可知,盡管我們指定了GET方法,F(xiàn)eign依然會(huì)使用POST方法發(fā)送請(qǐng)求。
正確寫法如下:
(1) 方法一
@FeignClient(name = "microservice-provider-user")
public interface UserFeignClient {
 @RequestMapping(value = "/get", method = RequestMethod.GET)
 public User get1(@RequestParam("id") Long id, @RequestParam("username") String username);
}這是最為直觀的方式,URL有幾個(gè)參數(shù),F(xiàn)eign接口中的方法就有幾個(gè)參數(shù)。使用@RequestParam注解指定請(qǐng)求的參數(shù)是什么。
(2) 方法二
多參數(shù)的URL也可使用Map來(lái)構(gòu)建。當(dāng)目標(biāo)URL參數(shù)非常多的時(shí)候,可使用這種方式簡(jiǎn)化Feign接口的編寫。
@FeignClient(name = "microservice-provider-user")
public interface UserFeignClient {
 @RequestMapping(value = "/get", method = RequestMethod.GET)
 public User get2(@RequestParam Map<String, Object> map);
}在調(diào)用時(shí),可使用類似以下的代碼。
public User get(String username, String password) {
 HashMap<String, Object> map = Maps.newHashMap();
 map.put("id", "1");
 map.put("username", "張三");
 return this.userFeignClient.get2(map);
}POST請(qǐng)求包含多個(gè)參數(shù)
下面我們來(lái)討論如何使用Feign構(gòu)造包含多個(gè)參數(shù)的POST請(qǐng)求。假設(shè)服務(wù)提供者的Controller是這樣編寫的:
@RestController
public class UserController {
 @PostMapping("/post")
 public User post(@RequestBody User user) {
  ...
 }
}我們要如何使用Feign去請(qǐng)求呢?答案非常簡(jiǎn)單,示例:
@FeignClient(name = "microservice-provider-user")
public interface UserFeignClient {
 @RequestMapping(value = "/post", method = RequestMethod.POST)
 public User post(@RequestBody User user);
}TIPS
(1) 本節(jié)相關(guān)代碼,詳見(jiàn)本書配套代碼中的microservice-provider-user-multiple-params項(xiàng)目和microservice-consumer-movie-feign-multiple-params項(xiàng)目。
(2) 除本節(jié)講解的方式外,我們也可編寫自己的編碼器來(lái)構(gòu)造多參數(shù)的請(qǐng)求,但這種方式編碼成本較高,代碼可重用性較低。故此,本書不再贅述。
拓展閱讀
(1) 希望Feign能夠支持參數(shù)請(qǐng)求使用POJO的Issue:https://github.com/spring-cloud/spring-cloud-netflix/issues/1253
(2) 建議使用Feign原生的注解的Issue:https://github.com/spring-cloud/spring-cloud-netflix/issues/659
(3) 建議增強(qiáng)Feign的功能:https://github.com/spring-cloud/spring-cloud-netflix/issues/1360
(4) 建議支持可選的Request Body(目前Feign當(dāng)POST一個(gè)null時(shí),會(huì)報(bào)異常):https://github.com/spring-cloud/spring-cloud-netflix/issues/1047
以上是“Spring Cloud怎么使用Feign構(gòu)造多參數(shù)的請(qǐng)求”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
                新聞標(biāo)題:SpringCloud怎么使用Feign構(gòu)造多參數(shù)的請(qǐng)求-創(chuàng)新互聯(lián)
                
                轉(zhuǎn)載來(lái)源:http://www.chinadenli.net/article42/djcehc.html
            
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、動(dòng)態(tài)網(wǎng)站、網(wǎng)站策劃、響應(yīng)式網(wǎng)站、網(wǎng)站收錄、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)