對于json對象類型(即JsonObject)的數據,springMVC主要有以下幾種方式接收:
1.通過Map接收
1
2
3
4
5
6
|
@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對象中來接收
1
2
3
4
5
6
7
8
9
10
|
@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數據的總結就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/wangpei555/article/details/72854921