SpringCloud通过Feign传递List类型参数方式
目录
- 通过Feign传递List类型参数
- 1、单个List实体传递
- 2、基本类型传递
- 3、实体类型传递
- Feign在参数为List时的坑
- 错误写法
- 正确写法
通过Feign传递List类型参数
首先明确一点,SpringCloud通过Fegin如果是多个参数,其中一个参数是List,那么是传不过去的,单个List是可以的。
1、单个List实体传递
@RequestMapping("/secret/batchInsert")
public int batchInsert(@RequestBody List<BatchSecretBO> batchSecretBOList){
return batchSecretService.batchInsert(batchSecretBOList);
}
2、基本类型传递
基本类型可以通过数组的方式传递,代码如下所示:
@RequestMapping(value = "/stat/merchant/get_merchant_compare_info", method = RequestMethod.POST)
@ResponseBody
MerchantCompareTotalInfo getMerchantCompareInfo(@RequestParam("licenseNoList") String[] licenseNoList);
3、实体类型传递
实体类型可以通过FastJson将List转换为String之后进行传递,代码如下:
//调用方代码
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数据
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只能通过以上方法转换成字符串后,再进行参数传递。
Feign在参数为List时的坑
我们在使用Feign进行服务接口调用时,有时候会有接口参数为List集合的时候,不能使用List接口类作为参数,只能用List的实现类。
错误写法

正确写法

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。
赞 (0)
