zoukankan      html  css  js  c++  java
  • Swagger入参为List

    今天遇到一个奇葩的需求,为了方便,service的一个接口入参是List<Map>
    repayMoney(List<Map<String,Object>)
    那,我为了在controller调用service,入参类型得一致呀,
    那么问题来了,controller入参也得是List吧,或者是其他形式的数据,转换为List<Map>
    翻遍了整个项目,没看到之前有人swagger的controller里入参是List的,而且查了一下,
    用@ApiImplicitParams注解的话,入参只能是String或者Integer,啊,这可怎么办,类似于这样
    关注点在入参为List,这个测试写的不完善,大家只要关注入参就可以了
    @ApiOperation(value = "测试")
        @RequestMapping(value = "/sanbiaoRepaymentAmount",method = RequestMethod.POST)
        @ApiImplicitParams({@ApiImplicitParam(name = "idCard", dataType = "String", value = "身份证号", paramType = "query", required = true),
                @ApiImplicitParam(name = "userNo", dataType = "Integer", value = "编号", paramType = "query", required = true),
                @ApiImplicitParam(name = "onePeriodAmount", dataType = "String", value = "金额", paramType = "query", required = true)})
        @ResponseBody
        public Result sanbiaoRepaymentAmount(@RequestParam(value = "idCard", required = true) String idCard,
                                             @RequestParam(value = "userNo", required = true) Integer userNo,
                                             @RequestParam(value = "onePeriodAmount", required = true) String onePeriodAmount) {
            Result result = new ResultSupport();
            List<Map<String,Object>> repayList = new ArrayList<Map<String,Object>>();
            Map paramMap = new HashMap();
            paramMap.put("idCard",idCard);
            paramMap.put("userNo",userNo);
            paramMap.put("onePeriodAmount",onePeriodAmount);
            repayList.add(paramMap);
            userService.repayMoney(repayList);
            return result;
        }
    

      

    好吧,查了一下,好像去掉ApiImplicitParams,就可以传list,
    于是 我做了如下修改
     
    @ApiOperation(value = "测试")
        @RequestMapping(value = "/sanbiaoRepaymentAmount",method = RequestMethod.POST)
        @ResponseBody
        public Result sanbiaoRepaymentAmount(@RequestParam(value = "idCardList", required = true) List<String> idCardList,
                                             @RequestParam(value = "userNoList", required = true) List<Integer> userNoList,
                                             @RequestParam(value = "onePeriodAmountList", required = true) List<String> onePeriodAmountList) {
    
            Result result = new ResultSupport();
            List<Map<String,Object>> repayList = new ArrayList<Map<String,Object>>();
            Map paramMap = new HashMap();
            for(int i=0; i<idCardList.size(); i++){
                //因为这里十分确定,三个入参list的长度是一致的,所以,在这里,把三个List对应的下标参数放在map里
                paramMap.put("idCard",userNoList.get(i));
                paramMap.put("userNo",onePeriodAmountList.get(i));
                paramMap.put("onePeriodAmount",onePeriodAmountList.get(i));
                repayList.add(paramMap);
            }
            System.out.println("haha");
            System.out.println(repayList);
         System.out.println(repayList.getClass());
            userService.repayMoney(repayList);
            return result;
        }
    

      

    开始测试--Swagger入参是这样得,看吧,DataType 是Array,数组呗

    逗号分隔参数,就可以咯

    顺利经过测试,能走到repayMoney()

  • 相关阅读:
    SpringMVC——拦截器及文件上传和下载
    SpringMVC——数据处理
    SpringMVC——返回结果及指定视图
    SpringMVC——SpringMVC配置
    SpringMVC——简介及原理简单分析
    Kakfa和其他类型消息中间件对比图
    Kafka消费者——结合spring开发
    Kafka生产者——结合spring开发
    五大浏览器-四大内核
    python中查看module和function的方法
  • 原文地址:https://www.cnblogs.com/coisini/p/9720865.html
Copyright © 2011-2022 走看看