首页 > 编程 > Java > 正文

浅谈springMVC接收前端json数据的总结

2019-11-26 10:08:54
字体:
来源:转载
供稿:网友

对于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格式传输数据。

这篇浅谈springMVC接收前端json数据的总结就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表