@RequestBody和@RequestParam的区别
- @RequestParam,主要处理contentType为application/x-www-form-urlencoded的数据(默认);@ResponseBody:主要处理contentType不为application/x-www-form-urlencoded的数据,例如:application/json;charset=UTF-8
- @RequestParam:要指明前端传过来的参数名并与其对应;@RequestBody:直接对象接收,属性名要与前端传过来的数据的key值对应
- 使用@RequestParam:Content-Type为application/x-www-form-urlencoded,参数在FormData中;使用@RequestBody:Content-Type为application/json,参数在Request PayLoad中
- 可以使用多个@RequestParam获取数据;@RequestBody不能在同一个方法中出现多次
数组参数
前端
var moduleids = moduleArr.join(','); //一定要切换成,分割的字符串传到后台
后台
@RequestParam List<String> moduleids //或者String[] moduleids都可以接受,分割的字符串
不定个数参数
前端
layui框架
//获取通讯方式并自动填充之前的配置 $.ajax({ url: "./warnwayjoinperson/getWarnwayForPerson" , data: {pcode:rowdata.code} , type: "post" , dataType: "json" , success: function (data) { var str = ''; $.each(data, function (index, d) { str += '<div class="layui-form-item">' +'<label class="layui-form-label">' +d.name +'</label>' +'<div class="layui-input-block">' +'<input type="text" name="'+d.code+'" value="'+(d.value==null?"":d.value)+'" autocomplete="off" class="layui-input">' +'</div>' +'</div>'; }); //放入html中 $('#warnwayDiv').html(str); //渲染表单 layui.use('form', function(){ var form = layui.form; form.render("checkbox"); //渲染checkbox }); } ,error:function(e){ layer.alert("请求失败",{title:'提示',icon: 2}); } });
后台
//接受参数qq:814,wechat:aeolian1995,phone:1535846 @RequestParam Map<String,String> paramMap
对象集合
对象集合需要先转换成json数据再传到后台。这种方式只能传一个json集合参数。
前端
前端通过JSON.stringify()函数将数组对象转换为json字符串。
$.ajax({ url: "../../back/teacherManager/addBatch" , data: JSON.stringify(teacherData) //数组转换成json字符串,json对象传输不需要key值,后台用@RequestBody接收即可. , contentType: "application/json;charset=UTF-8" //复杂对象集合,一定要用application/json. , type: "post" , dataType: "json" , success: function (data) { if (data.code == "success") { layer.alert(data.msg,{title:'提示',icon: 1},function(index){ layer.close(index); }); $("#form_upload")[0].reset(); }else { layer.alert(data.msg,{title:'提示',icon: 2}); } } });
后台
后端参数用@RequestBody接受前端ajax用application/json传输的复杂对象集合。
/** * 批量导入 * @param teacherMap * @param session * @return */ @RequestMapping(value="/addBatch",method={RequestMethod.GET,RequestMethod.POST}) @ResponseBody public Object addBatch(@RequestBody List<Map> teacherMap, HttpSession session){ List<TeacherManagerEntity> teacherList = new ArrayList<TeacherManagerEntity>(); TeacherManagerEntity t = null; for (Map m:teacherMap){ String name = String.valueOf(m.get("name")); String phone = String.valueOf(m.get("phone")); String facultyName = String.valueOf(m.get("facultyName")); String className = String.valueOf(m.get("className")); t = new TeacherManagerEntity(); t.setName(name); t.setPhone(phone); t.setFacultyName(facultyName); t.setClassName(className); t.setType("3"); t.setLoginName(phone); t.setPwd("96e79218965eb72c92a549dd5a330112"); teacherList.add(t); } int[] result = teacherService.addBatch(teacherList); /*计算总和*/ int sum=0; for(int i=0;i<result.length;i++) { sum=sum+result[i]; } Map map = new HashMap(); if (sum>0) { map.put("code", "success"); map.put("msg", "添加成功!导入教师信息"+sum+"条,默认密码111111"); }else { map.put("code", "fail"); map.put("msg", "添加失败!"); } return map; }
如图
如果想传其他参数,则需要把json集合以字符串方式传到后台用Gson或者fastjson转换。