SpringCloud通过Feign传递List类型参数

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_19734597/article/details/102555424

首先明确一点,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只能通过以上方法转换成字符串后,再进行参数传递。

猜你喜欢

转载自blog.csdn.net/qq_19734597/article/details/102555424