對於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數據的總結就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。