对于json对象类型(即JsonObject)的数据,springMVC主要有以下几种方式接收:
1.通过Map接收
@RequestMapping(value = "/getAllStudio" )
public void getAllStudio(@RequestBody Map<String, Integer> map ) {
JSONObject json = new JSONObject();
Integer page = map.get("page") ;// 当前页
Integer rows = map.get("rows") ;// 每页显示的数量
}
2.通过将数据封装在一个vo对象中来接收
@RequestMapping(value = "/addStudio")
public JSONObject addStudio(@RequestBody Studio stu) throws IOException {
JSONObject json = new JSONObject();
if(stu==null){
json.put("result",false);
return json;
}
}
“`
补充:几种常见的post传输数据的方式
在传输http请求时,Content-Type 字段来获知请求中的消息主体是用何种方式编码
1.application/x-www-form-urlencoded
表单提交的方式,其传输的数据会被转换为data1=1&data2=2的形式。
在controller层可通过request.getParametre(“data1”);获取。
Ajax提交数据时,一般也采用该形式。
2.multipart/form-data
多文件上传时指定的格式。
3.application/json
以json格式传输数据。