springcloud feign調(diào)其他微服務(wù)時參數(shù)是對象的問題
在使用feign調(diào)用其它服務(wù)時,發(fā)現(xiàn)獲取的參數(shù)是null,當(dāng)參數(shù)是對象是,是執(zhí)行的Post請求,所以要在方法參數(shù)前加@RequestBody,
@RequestBody
處理HttpEntity傳遞過來的數(shù)據(jù),一般用來處理非Content-Type: application/x-www-form-urlencoded編碼格式的數(shù)據(jù)。
GET請求中,因?yàn)闆]有HttpEntity,所以@RequestBody并不適用。POST請求中,通過HttpEntity傳遞的參數(shù),必須要在請求頭中聲明數(shù)據(jù)的類型Content-Type,SpringMVC通過使用HandlerAdapter 配置的HttpMessageConverters來解析HttpEntity中的數(shù)據(jù),然后綁定到相應(yīng)的bean上。
GET請求多參數(shù)的URL
假設(shè)我們請求的URL包含多個參數(shù),例如http://microservice-provider-user/get?id=1&username=張三 ,要怎么辦呢?
我們知道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);
}然而我們測試時會發(fā)現(xiàn)該寫法不正確,我們將會收到類似以下的異常:
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依然會發(fā)送POST請求。
正確寫法如下
(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) 方法二
@FeignClient(name = "microservice-provider-user")
public interface UserFeignClient {
? @RequestMapping(value = "/get", method = RequestMethod.GET)
? public User get2(@RequestParam Map<String, Object> map);
}多參數(shù)的URL也可以使用Map去構(gòu)建。當(dāng)目標(biāo)URL參數(shù)非常多的時候,可使用這種方式簡化Feign接口的編寫。
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);
}feign接口調(diào)用其他微服務(wù)中參數(shù)是集合對象(List<Java對象>)且請求方式是PUT或者POST方式的解決
首先,如果傳輸?shù)氖羌蠈ο?,一般的不是PUT或者POST請求都是可以用@RequestParam("…")的形式寫在接口的新參中,比如
@GetMapping("/find/sec/consume/product/category")
public ResponseEntity<List<SecConsumeProductCategoryVO>> getSecConsumeProductCategory(@RequestParam("sellerIds") List<Long> sellerIds){
? ? List<SecConsumeProductCategoryVO> secConsumeProductCategories = secConsumeProductBaseBusinessService.getSecConsumeProductCategory(sellerIds);
? ? return ResponseEntity.ok(secConsumeProductCategories);
}而對于feign調(diào)用且參數(shù)是集合對象的情況, 在feign客戶端,則可以使用如下方式,請求路勁的注解就不能直接使用@PutMapping或者@PostMapping了,而必須使用@RequestMapping,形參仍然使用注解@RequestBody
@RequestMapping(value = "/cancel/daily/appointment",method = RequestMethod.PUT) public Void updateBatchDailyAppointment(@RequestBody List<PdProductDailyAppointmentDTO> cancelAppointmentDTOS/*String cancelAppointmentStr*/);
而對于被調(diào)用方,則可以這樣寫
@RequestMapping(value = "/cancel/daily/appointment",method = RequestMethod.PUT)
public ResponseEntity<Void> updateBatchDailyAppointment(@RequestBody List<PdProductDailyAppointmentDTO> cancelAppointmentDTOS/*String cancelAppointmentStr*/){
? ? pdProductDailyAppointmentBusinessService.updateBatchDailyAppointment(cancelAppointmentDTOS);
? ? return ResponseEntity.status(HttpStatus.CREATED).build();
}這樣,就可以解決feign調(diào)用傳輸?shù)氖羌蠈ο蟮膯栴}啦
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring?Boot?整合?Reactor實(shí)例詳解
這篇文章主要為大家介紹了Spring?Boot?整合?Reactor實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
SpringBoot詳細(xì)講解視圖整合引擎thymeleaf
這篇文章主要分享了Spring Boot整合使用Thymeleaf,Thymeleaf是新一代的Java模板引擎,類似于Velocity、FreeMarker等傳統(tǒng)引擎,關(guān)于其更多相關(guān)內(nèi)容,需要的小伙伴可以參考一下2022-06-06
UrlDecoder和UrlEncoder使用詳解_動力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了UrlDecoder和UrlEncoder使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
基于Spring實(shí)現(xiàn)自定義錯誤信息返回詳解
這篇文章主要為大家詳細(xì)介紹了如何基于Spring實(shí)現(xiàn)自定義錯誤信息返回效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-03-03
SpringBoot整合liquibase及l(fā)iquibase生成初始化腳本的方式
這篇文章主要介紹了SpringBoot整合liquibase的相關(guān)資料,文中給大家介紹了liquibase生成初始化腳本的兩種方式,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02
SpringBoot數(shù)據(jù)校驗(yàn)功能的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot數(shù)據(jù)校驗(yàn)功能的實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02
Android bdflow數(shù)據(jù)庫神器的使用
這篇文章主要介紹了Android bdflow數(shù)據(jù)庫神器的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03

