Feign传递List类型参数

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Feign传递List类型参数

张志翔 ̮   2022-05-28 我要评论

通过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的实现类。

错误写法

正确写法

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们