springcloud feign調(diào)其他微服務(wù)時(shí)參數(shù)是對(duì)象的問題
在使用feign調(diào)用其它服務(wù)時(shí),發(fā)現(xiàn)獲取的參數(shù)是null,當(dāng)參數(shù)是對(duì)象是,是執(zhí)行的Post請(qǐng)求,所以要在方法參數(shù)前加@RequestBody,
@RequestBody
處理HttpEntity傳遞過來的數(shù)據(jù),一般用來處理非Content-Type: application/x-www-form-urlencoded編碼格式的數(shù)據(jù)。
GET
請(qǐng)求中,因?yàn)闆]有HttpEntity,所以@RequestBody并不適用。POST
請(qǐng)求中,通過HttpEntity傳遞的參數(shù),必須要在請(qǐng)求頭中聲明數(shù)據(jù)的類型Content-Type,SpringMVC通過使用HandlerAdapter 配置的HttpMessageConverters來解析HttpEntity中的數(shù)據(jù),然后綁定到相應(yīng)的bean上。
GET請(qǐng)求多參數(shù)的URL
假設(shè)我們請(qǐng)求的URL包含多個(gè)參數(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); }
然而我們測(cè)試時(shí)會(huì)發(fā)現(xiàn)該寫法不正確,我們將會(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ì)發(fā)送POST請(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) 方法二
@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ù)非常多的時(shí)候,可使用這種方式簡(jiǎn)化Feign接口的編寫。
POST請(qǐng)求包含多個(gè)參數(shù)
下面我們來討論如何使用Feign構(gòu)造包含多個(gè)參數(shù)的POST請(qǐng)求。舉個(gè)例子,假設(shè)我們的用戶微服務(wù)的Controller是這樣編寫的:
@RestController public class UserController { ? @PostMapping("/post") ? public User post(@RequestBody User user) { ? ? ... ? } }
我們的Feign接口要如何編寫呢?答案非常簡(jiǎn)單,示例:
@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ù)是集合對(duì)象(List<Java對(duì)象>)且請(qǐng)求方式是PUT或者POST方式的解決
首先,如果傳輸?shù)氖羌蠈?duì)象,一般的不是PUT或者POST請(qǐng)求都是可以用@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); }
而對(duì)于feign調(diào)用且參數(shù)是集合對(duì)象的情況, 在feign客戶端,則可以使用如下方式,請(qǐng)求路勁的注解就不能直接使用@PutMapping或者@PostMapping了,而必須使用@RequestMapping,形參仍然使用注解@RequestBody
@RequestMapping(value = "/cancel/daily/appointment",method = RequestMethod.PUT) public Void updateBatchDailyAppointment(@RequestBody List<PdProductDailyAppointmentDTO> cancelAppointmentDTOS/*String cancelAppointmentStr*/);
而對(duì)于被調(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ù)氖羌蠈?duì)象的問題啦
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Jenkins系統(tǒng)如何進(jìn)行數(shù)據(jù)備份
隨著我們的長(zhǎng)期使用,Jenkins系統(tǒng)中的內(nèi)容會(huì)越來越多,特別是一些配置相關(guān)的東西,不能有任何丟失。這個(gè)時(shí)候我們就需要定期備份我們的Jenkins系統(tǒng),避免一些誤操作不小心刪除了某些重要文件,本文就將介紹下Jenkins系統(tǒng)如何進(jìn)行數(shù)據(jù)備份2021-06-06java編程創(chuàng)建型設(shè)計(jì)模式工廠方法模式示例詳解
這篇文章主要為大家介紹了java編程創(chuàng)建型設(shè)計(jì)模式之工廠方法模式的創(chuàng)建及案例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-02-02Java中語(yǔ)音url轉(zhuǎn)換成InputStream的示例代碼
在Java中,可以使用java.net.URL和java.net.URLConnection類來將語(yǔ)音URL轉(zhuǎn)換為InputStream,本文通過示例代碼介紹Java中語(yǔ)音url轉(zhuǎn)換成InputStream的相關(guān)知識(shí),感興趣的朋友一起看看吧2024-01-01Java服務(wù)限流算法的6種實(shí)現(xiàn)
服務(wù)限流是指通過控制請(qǐng)求的速率或次數(shù)來達(dá)到保護(hù)服務(wù)的目的,本文主要介紹了Java服務(wù)限流算法的6種實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-05-05