Springboot接收ajax提交JSON数组

简介: Springboot接收ajax提交JSON数组


  1. AJAX传递JSON数据
function save() {
        var data = JSON.stringify({d_date: "2019-06-06",vc_exchange : "IB",vc_symbol:"038007",vc_type:"KRD",vc_source:"test"})
        $.ajax({
            type : "post",
            url : "/bloomberg/save",
            data : data,
            contentType: 'application/json',
            success : function (result) {
                console.log(result);
            }
        })
    };
@RequestMapping(value="/save",method = RequestMethod.POST,consumes = "application/json")
@ResponseBody
public String save(@RequestBody XxxBean bean){
    return "success";
}
  1. AJAX传递JSON数组
var data = [{d_date: "2019-06-06",vc_exchange : "IB",vc_symbol:"038007",vc_type:"KRD",vc_source:"test"},
      {d_date: "2019-06-05",vc_exchange : "IB",vc_symbol:"038006",vc_type:"KRD",vc_source:"cs"}];
        $.ajax({
            type : "post",
            url : "/bloomberg/saveAll",
            data : JSON.stringify({beans:data}),
            contentType: 'application/json',
            success : function (result) {
                console.log(result);
            }
        })
    };

不能直接使用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<XxxBean> list=(List)JSONArray.toCollection(jsonArray,XxxBean.class);
    xxxService.saveAll(list);
    return "successAll";
}


相关文章
|
3月前
|
前端开发 Java
SpringBoot之数组,集合,日期参数的详细解析
SpringBoot之数组,集合,日期参数的详细解析
74 0
|
1月前
|
JSON 数据格式
MysbatisPlus-核心功能-IService开发基础业务接口,MysbatisPlus_Restful风格,新增@RequestBody指定是为了接收Json数据的,使用swagger必须注解
MysbatisPlus-核心功能-IService开发基础业务接口,MysbatisPlus_Restful风格,新增@RequestBody指定是为了接收Json数据的,使用swagger必须注解
|
2月前
|
XML JSON 前端开发
第十一篇JavaScript JSON与AJAX
第十一篇JavaScript JSON与AJAX
20 0
|
3月前
|
XML JSON 前端开发
Ajax – JSON入门指南
Ajax – JSON入门指南
29 1
|
3月前
|
JSON 前端开发 JavaScript
jQuery ajax读取本地json文件 三级联动下拉框
jQuery ajax读取本地json文件 三级联动下拉框
|
3月前
|
JSON JavaScript Java
从前端Vue到后端Spring Boot:接收JSON数据的正确姿势
从前端Vue到后端Spring Boot:接收JSON数据的正确姿势
160 0
|
3月前
|
XML JSON 前端开发
|
3月前
|
XML JSON 前端开发
|
3月前
|
JSON 前端开发 JavaScript
Vue+Axios+SpringBoot后端同时接收文件和json作为请求参数
Vue+Axios+SpringBoot后端同时接收文件和json作为请求参数
209 0
|
9月前
|
XML 前端开发 JavaScript
什么是Ajax和jquery
什么是Ajax和jquery
70 0