對于json對象類型(即JsonObject)的數(shù)據(jù),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") ;// 當(dāng)前頁 Integer rows = map.get("rows") ;// 每頁顯示的數(shù)量 } |
2.通過將數(shù)據(jù)封裝在一個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; } } |
補(bǔ)充:幾種常見的post傳輸數(shù)據(jù)的方式
在傳輸http請求時,Content-Type 字段來獲知請求中的消息主體是用何種方式編碼
1.application/x-www-form-urlencoded
表單提交的方式,其傳輸?shù)臄?shù)據(jù)會被轉(zhuǎn)換為data1=1&data2=2的形式。
在controller層可通過request.getParametre(“data1”);獲取。
Ajax提交數(shù)據(jù)時,一般也采用該形式。
2.multipart/form-data
多文件上傳時指定的格式。
3.application/json
以json格式傳輸數(shù)據(jù)。
這篇淺談springMVC接收前端json數(shù)據(jù)的總結(jié)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/wangpei555/article/details/72854921