溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Spring Cloud怎么使用Feign構(gòu)造多參數(shù)的請求

發(fā)布時間:2021-02-18 12:52:21 來源:億速云 閱讀:377 作者:小新 欄目:編程語言

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

GET請求多參數(shù)的URL

假設(shè)我們請求的URL包含多個參數(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);
}

然而,這種寫法并不正確,控制臺會輸出類似如下的異常。

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依然會使用POST方法發(fā)送請求。

正確寫法如下:

(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有幾個參數(shù),F(xiàn)eign接口中的方法就有幾個參數(shù)。使用@RequestParam注解指定請求的參數(shù)是什么。

(2) 方法二

多參數(shù)的URL也可使用Map來構(gòu)建。當(dāng)目標(biāo)URL參數(shù)非常多的時候,可使用這種方式簡化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)用時,可使用類似以下的代碼。

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請求包含多個參數(shù)

下面我們來討論如何使用Feign構(gòu)造包含多個參數(shù)的POST請求。假設(shè)服務(wù)提供者的Controller是這樣編寫的:

@RestController
public class UserController {
 @PostMapping("/post")
 public User post(@RequestBody User user) {
  ...
 }
}

我們要如何使用Feign去請求呢?答案非常簡單,示例:

@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)代碼,詳見本書配套代碼中的microservice-provider-user-multiple-params項目和microservice-consumer-movie-feign-multiple-params項目。

(2) 除本節(jié)講解的方式外,我們也可編寫自己的編碼器來構(gòu)造多參數(shù)的請求,但這種方式編碼成本較高,代碼可重用性較低。故此,本書不再贅述。

拓展閱讀

(1) 希望Feign能夠支持參數(shù)請求使用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) 建議增強Feign的功能:https://github.com/spring-cloud/spring-cloud-netflix/issues/1360
(4) 建議支持可選的Request Body(目前Feign當(dāng)POST一個null時,會報異常):https://github.com/spring-cloud/spring-cloud-netflix/issues/1047

以上是“Spring Cloud怎么使用Feign構(gòu)造多參數(shù)的請求”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI