SpringCloud通過Feign傳遞List類型參數(shù)方式
通過Feign傳遞List類型參數(shù)
首先明確一點,SpringCloud通過Fegin如果是多個參數(shù),其中一個參數(shù)是List,那么是傳不過去的,單個List是可以的。
1、單個List實體傳遞
@RequestMapping("/secret/batchInsert")
public int batchInsert(@RequestBody List<BatchSecretBO> batchSecretBOList){
? ? return batchSecretService.batchInsert(batchSecretBOList);
}2、基本類型傳遞
基本類型可以通過數(shù)組的方式傳遞,代碼如下所示:
@RequestMapping(value = "/stat/merchant/get_merchant_compare_info", method = RequestMethod.POST)
@ResponseBody
MerchantCompareTotalInfo getMerchantCompareInfo(@RequestParam("licenseNoList") String[] licenseNoList);3、實體類型傳遞
實體類型可以通過FastJson將List轉(zhuǎn)換為String之后進行傳遞,代碼如下:
//調(diào)用方代碼
String contracts = JSONObject.toJSONString(contractBOList);
contractDao.contractBatchSetRedis(contracts , 60 * 60);
?
//接收方代碼
@PostMapping("/contract/contractBatchSetRedis")
void contractBatchSetRedis(@RequestParam("contractBOList") String contractBOList, @RequestParam("expire") long expire) {
? ? List<ContractBO> contracts = JSONObject.parseArray(contractBOList, ContractBO.class);
? ? if (contracts == null || contracts.size() == 0) {
? ? ? ? ?return;
? ? }
? ? //批量set數(shù)據(jù)
? ? redisUtil.getRedisTemplate().executePipelined((RedisCallback<String>) connection -> {
? ? ? ? for (ContractBO contract : contracts) {
? ? ? ? ? ? connection.setEx((RedisPrefixConst.CONTRACT_PREFIX + contract.getBusinessCode() + RedisPrefixConst.UNDERLINE_SEPARATOR + contract.getContractNo()).getBytes(), expire, JSONObject.toJSONString(contract).getBytes());
? ? ? ? }
? ? ? ? return null;
? ? });
}fegin局限性較多,如果要傳遞List只能通過以上方法轉(zhuǎn)換成字符串后,再進行參數(shù)傳遞。
Feign在參數(shù)為List時的坑
我們在使用Feign進行服務接口調(diào)用時,有時候會有接口參數(shù)為List集合的時候,不能使用List接口類作為參數(shù),只能用List的實現(xiàn)類。
錯誤寫法

正確寫法

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
spring boot + mybatis實現(xiàn)動態(tài)切換數(shù)據(jù)源實例代碼
這篇文章主要給大家介紹了關于spring boot + mybatis實現(xiàn)動態(tài)切換數(shù)據(jù)源的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-10-10
SpringBoot中@ComponentScan的使用詳解
這篇文章主要介紹了SpringBoot中@ComponentScan的使用詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
java中 String和StringBuffer的區(qū)別實例詳解
這篇文章主要介紹了java中 String和StringBuffer的區(qū)別實例詳解的相關資料,一個小的例子,來測試String和StringBuffer在時間和空間使用上的差別,需要的朋友可以參考下2017-04-04
springboot處理url中帶斜杠/\字符的參數(shù)報400問題
這篇文章主要介紹了springboot處理url中帶斜杠/\字符的參數(shù)報400問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
解決后端傳long類型數(shù)據(jù)到前端精度丟失問題
這篇文章主要介紹了解決后端傳long類型數(shù)據(jù)到前端精度丟失問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01

