前端传输map集合数据到后台

js:

function createUrl() {
    var map = {};
    $("#channelTable").bootstrapTable('getSelections').forEach(function (item) {
        map[item.id]=item.number;
    });
    if (map.size <= 0) {
        toastr.error("请至少选择一行记录");
        return;
    }

    $.ajax({
        url: 'channel/createChannelUrl',
        method: 'post',
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(map),
        success: function () {
            toastr.success("添加渠道url成功");
            $("#channelTable").bootstrapTable('refresh');
        },
        error: function (err) {
            if (400 === err.status) {
                toastr.error(err.responseText);
            } else {
                toastr.error('添加渠道信息失败')
            }
        }
    });
}

controller:

   @PostMapping("createChannelUrl")
    public ResponseEntity createChannelUrl(@RequestBody Map map) {
      channelService.createChannelUrl(map);
        return ResponseEntity.ok().build();
    }

service:

 @Override
    public 	int createChannelUrl( Map map){
        //遍历map
        for(Map.Entry entry : map.entrySet()){
            String mapKey = entry.getKey();
            String mapValue = entry.getValue();
            JsonArray jsonArray = HttpRequestUtil.getXpath(mapValue);
            
            List channelUrlList = new ArrayList();
            log.info(jsonArray.toString()+jsonArray.size());

            for (int i = 0; i < jsonArray.size(); i++) {
                log.info(jsonArray.get(i).toString());
                JsonObject jsonObject = (JsonObject) jsonArray.get(i);
                channelUrlList.add(new ChannelUrl()
                        .setCategory("0")
                        .setDownCount("0")
                        .setUrl(jsonObject.get("url").toString().replaceAll("\"",""))
                        .setChannelId(Integer.parseInt(mapKey))
                        .setJson(jsonObject.toString())
                        .setKwd(jsonObject.get("kwd").toString().replaceAll("\"",""))
                );

            };
            return channelUrlMapper.insert(channelUrlList);
        }

        return 1;
    }

你可能感兴趣的:(---javaweb)