spring mvc 傳遞複雜的物件
在spring mvc 傳遞簡單物件時候,是比較簡單的,
但是,在傳遞List 等,集合時,初學者,可能有點麻煩,
http://blog.csdn.net/lutinghuan/article/details/46820023
http://blog.csdn.net/wabiaozia/article/details/50803581
http://blog.csdn.net/u011781521/article/details/77586688
http://blog.csdn.net/jhq2904982765/article/details/77141618
上面是我,解決各種問題.所需啊喲的blg
其中,前兩篇,可以解決大部問題,
最後,一篇,可以解決,全部問題
,接下來,我描述下我的問題
這是我car 物件的屬性,其中有兩個 SET 集合
https://gitee.com/echo_yi/cars/blob/master/src/main/java/com/org/cars/domain/Car.java
對於這個問題,前段採用json 傳輸,但是,注意格式
jQuery("form").serialize(); //"username=&password=" String
jQuery("form").serializeArray(); //[{name:"username",value:""},{name:"password",value:""}] shuzu
jQuery("form").serializeObject(); //{username:"",password:""}
JSON.stringify(jsondata)
$.toJSON(customerArray),
要理解,每一種的狀態,之前,我直接就是直接序列 serialize(),導致出錯
這是我的 js
$(document).ready(function(){ submitUserList_3(); function submitUserList_3() { var car ={ "id" : 1, "name" : "2018款 730Li 領先型 M運動套裝", "price" : 28.88, "brand" : "賓士", "model" : "7系", "mileage" : 11.4, "vehicleOfAge": 5, "numberOfHouseholds" : 0, "emissionOfStandard" : "國2", "checkOfexpires" : "2017-11-29", "levelOfType" : "家用", "displacement" : 2.0, "nationality" : "德國", "bodyOfType" : "二廂", "driverOfType" : "後驅", "pictures" : [ { "id" : 4, "type" : "interior", "address" : "/0395b396-8b7c-4d1b-a2c9-eb8aea2ee30b.png" }, { "id" : 1, "type" : "details", "address" : "/50b860fd-9f4b-4a0e-aef0-f5654bc7262f.png" }, { "id" : 3, "type" : "details", "address" : "/0a830b74-5073-4d1e-8436-f431d52ba6c0.png" }, { "id" : 5, "type" : "details", "address" : "/82e27b36-b81c-4ac2-9779-88f6c02c68cb.png" }, { "id" : 9, "type" : "appearance", "address" : "/ba3698dc-cef4-405e-9226-3f63e2d55985.png" }, { "id" : 7, "type" : "appearance", "address" : "/b9548679-f8cf-4d8a-8180-3669a0591189.png" }, { "id" : 8, "type" : "interior", "address" : "/244cd446-b25f-4673-9325-61e650166467.png" }, { "id" : 11, "type" : "appearance", "address" : "/d1dd4cc8-470d-45b4-a158-14ecc9c36077.png" }, { "id" : 2, "type" : "interior", "address" : "/4bd801f9-9c7c-47da-a53e-4729f2a83639.png" }, { "id" : 10, "type" : "interior", "address" : "/7e82bc86-f331-4b57-9d3c-51acd56d82e9.png" }, { "id" : 12, "type" : "appearance", "address" : "/2a8be682-166c-473d-891f-0e027053799b.png" }, { "id" : 6, "type" : "details", "address" : "/d401ed1f-867f-4069-ba27-4541005a6b49.png" } ], "cardOfTime" : "2017-11-29", "strongOfInsurance" : "2017-11-29", "businessOfInsuranceOfExpires" : "2017-11-29", "interpretationOfCar" : [ { "id" : 1, "address" : "/a3f69b0f-0cca-42dd-922a-b2384ae76cb0.png", "interpretationOfInfo" : "upipoijououououoiuoiuoiuouoiuoiuoiuoiui " } ], "cardOfAddress" : "廣州", "transmissionOfType" : "手動", "fuelOfType" : "混合", "seatOfNumber" : 3, "releaseOfTime" : "2017-11-29" }; $.ajax({ url: "/twoone", type: "POST", contentType : 'application/json;charset=utf-8', //設定請求頭資訊 dataType:"json", //data: JSON.stringify(customerArray), //將Json物件序列化成Json字串,JSON.stringify()原生態方法 // data: $.toJSON(customerArray), //將Json物件序列化成Json字串,toJSON()需要引用jquery.json.min.js // data:$("#put").serialize(), // data:"model="+ddd, data:JSON.stringify(car), success: function(data){ console.log(data); console.log($("#put").serialize()); console.log(JSON.stringify(car)); }, error: function(res){ // alert(res.responseText); console.log(res.responseText); } }); } });
其中,對於car 物件的資料,我是採用,先從後臺輸出一個@Responsbody car,
直接運用 谷歌的postman 獲取資料,
然後放在js 裡面(因為對js 不熟,只好採用簡單的方法)
這是我的controller
@PostMapping(value="/twoone",produces = MediaType.APPLICATION_JSON_VALUE) // @Time // @RequestMapping(value = "/twoone") @ResponseBody public Car u(@RequestBody Car car){ System.out.println("-----------------"); System.out.println(car.getName()); // return carRepository.findOne(1); return car; }
特別是
@PostMapping(value="/twoone",produces = MediaType.APPLICATION_JSON_VALUE)
http://blog.csdn.net/jhq2904982765/article/details/77141618
宣告檔案,這個特別重要
,問題,解決
建議看下orgjson