100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > Springboot接收ajax提交JSON数组

Springboot接收ajax提交JSON数组

时间:2020-11-13 00:28:10

相关推荐

Springboot接收ajax提交JSON数组

AJAX传递JSON数据

function save() {var data = JSON.stringify({d_date: "-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")@ResponseBodypublic String save(@RequestBody XxxBean bean){return "success";}

AJAX传递JSON数组

var data = [{d_date: "-06-06",vc_exchange : "IB",vc_symbol:"038007",vc_type:"KRD",vc_source:"test"},{d_date: "-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")@ResponseBodypublic String saveAll(@RequestBody JSONObject jsonObject){JSONArray jsonArray = jsonObject.getJSONArray("beans");List<XxxBean> list=(List)JSONArray.toCollection(jsonArray,XxxBean.class);xxxService.saveAll(list);return "successAll";}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。