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";}