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

