在controller直接使用@requestBody注解接收List集合,指定泛型。直接使用List参数做处理时,会提示错误java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.syntheApp.vo.***
后端代码:
@RequestMapping(value="/saveRotaDeptProperty") public ResponseEntity<Map<String, Object>> saveRotaDeptProperty(@RequestBody List<RotaDeptProperty> rotaDeptPropertys){ Map<String, Object> resultJson = new HashMap<String, Object>(); boolean flag=false; try { flag=managementService.saveRotaDeptPropertyList(rotaDeptPropertys); if(flag){ resultJson.put(GlobalConstants.AJAX_RETURN_STATE, GlobalConstants.AJAX_RETURN_STATE_OK); }else{ resultJson.put(GlobalConstants.AJAX_RETURN_STATE, GlobalConstants.AJAX_RETURN_STATE_ERROR); } } catch (Exception e) { resultJson.put(GlobalConstants.AJAX_RETURN_STATE, GlobalConstants.AJAX_RETURN_STATE_ERROR); resultJson.put(GlobalConstants.AJAX_RETURN_MSG, e.getMessage()); logger.error(e); e.printStackTrace(); } HttpHeaders h = new HttpHeaders(); h.setContentType(MediaType.valueOf(GlobalConstants.HTTP_MEDIATYPE)); return new ResponseEntity<Map<String, Object>>(resultJson, h,HttpStatus.OK); }
前端代码:
//保存 function saveData(rotaDeptArray){ $.ajax({ type: "POST", url:"${base}/traineeManager/saveRotaDeptProperty.do", data: JSON.stringify(rotaDeptArray), contentType:"application/json", beforeSend:beforeSendCon, success: function(msg){ if(msg.state==3){ msgSaveSuccess(); setTimeout(function(){parent.layer.closeAll();},500); }else{ msgSavefail(); } } }); }
处理方式:
1、可以使用对象传参,将数组作为对象的一个属性,同时修改接收参数,与传参对象。
2、使用键值对传参,获取json字符串,将字符串解析成jsonArray对象,再转换成List。
方式二代码:
//保存 function saveData(rotaDeptArray){ $.ajax({ type: "POST", url:"${base}/traineeManager/saveRotaDeptProperty.do", data: { arrayStr:JSON.stringify(rotaDeptArray) }, beforeSend:beforeSendCon, success: function(msg){ if(msg.state==3){ msgSaveSuccess(); setTimeout(function(){parent.layer.closeAll();},500); }else{ msgSavefail(); } } }); }
@RequestMapping(value="/saveRotaDeptProperty") public ResponseEntity<Map<String, Object>> saveRotaDeptProperty(HttpServletRequest request){ //获取字符串 String arrayStr=request.getParameter("arrayStr"); //字符串转array数组 JSONArray array=JSONArray.fromObject(arrayStr); //数组转List集合 List<RotaDeptProperty> rotaDeptPropertys=JSONArray.toList(array, new RotaDeptProperty(), new JsonConfig()); Map<String, Object> resultJson = new HashMap<String, Object>(); boolean flag=false; try { flag=managementService.saveRotaDeptPropertyList(rotaDeptPropertys); if(flag){ resultJson.put(GlobalConstants.AJAX_RETURN_STATE, GlobalConstants.AJAX_RETURN_STATE_OK); }else{ resultJson.put(GlobalConstants.AJAX_RETURN_STATE, GlobalConstants.AJAX_RETURN_STATE_ERROR); } } catch (Exception e) { resultJson.put(GlobalConstants.AJAX_RETURN_STATE, GlobalConstants.AJAX_RETURN_STATE_ERROR); resultJson.put(GlobalConstants.AJAX_RETURN_MSG, e.getMessage()); logger.error(e); e.printStackTrace(); } HttpHeaders h = new HttpHeaders(); h.setContentType(MediaType.valueOf(GlobalConstants.HTTP_MEDIATYPE)); return new ResponseEntity<Map<String, Object>>(resultJson, h,HttpStatus.OK); }
还没有评论,来说两句吧...