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
請求中,因為沒有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ù)氖羌蠈ο螅话愕牟皇荘UT或者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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Jenkins系統(tǒng)如何進行數(shù)據(jù)備份
隨著我們的長期使用,Jenkins系統(tǒng)中的內(nèi)容會越來越多,特別是一些配置相關(guān)的東西,不能有任何丟失。這個時候我們就需要定期備份我們的Jenkins系統(tǒng),避免一些誤操作不小心刪除了某些重要文件,本文就將介紹下Jenkins系統(tǒng)如何進行數(shù)據(jù)備份2021-06-06java編程創(chuàng)建型設(shè)計模式工廠方法模式示例詳解
這篇文章主要為大家介紹了java編程創(chuàng)建型設(shè)計模式之工廠方法模式的創(chuàng)建及案例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-02-02Java中語音url轉(zhuǎn)換成InputStream的示例代碼
在Java中,可以使用java.net.URL和java.net.URLConnection類來將語音URL轉(zhuǎn)換為InputStream,本文通過示例代碼介紹Java中語音url轉(zhuǎn)換成InputStream的相關(guān)知識,感興趣的朋友一起看看吧2024-01-01