ajax提交JSON数组及Springboot接收转换为list类

1、AJAX传递JSON数据




    
    Title








    @RequestMapping(value="/save",method = RequestMethod.POST,consumes = "application/json")
    @ResponseBody
    public String save(@RequestBody XxxBean bean){
        System.out.println(bean);
        return "success";
    }

2、AJAX传递JSON数组




    
    Title








不能直接使用JSONArray类型的参数接收前台的json数组字符串。曲线救国:用JSONObject接收,然后取出JSONArray。

    @RequestMapping(value="/saveAll",method = RequestMethod.POST,consumes = "application/json")
    @ResponseBody
    public String saveAll(@RequestBody JSONObject jsonObject){
        JSONArray jsonArray = jsonObject.getJSONArray("beans");
        List list=(List)JSONArray.toCollection(jsonArray,XxxBean.class);
        xxxService.saveAll(list);
        return "successAll";
    }

你可能感兴趣的:(SpringBoot,2.x学习笔记)