SpringCloud通过Feign传递List类型参数方式

网友投稿 585 2022-08-20


SpringCloud通过Feign传递List类型参数方式

目录通过Feign传递List类型参数1、单个List实体传递2、基本类型传递3、实体类型传递Feign在参数为Lishttp://t时的坑错误写法正确写法

通过Feign传递List类型参数

首先明确一点,SpringCloud通过Fegin如果是多个参数,其中一个参数是List,那么是传不过去的,单个List是可以的。

1、单个List实体传递

@RequestMapping("/secret/batchInsert")

public int batchInsert(@RequestBody List 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 contracts = JSONObject.parseArray(contractBOList, ContractBO.class);

if (contracts == null || HKnjmVcontracts.size() == 0) {

return;

}

//批量set数据

redisUtil.getRedisTemplate().executePipelined((RedisCallback) 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的实现类。

错误写法

正确写法


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java理论基础Stream性能论证测试示例
下一篇:Java如何判断线程是否结束的三种方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~