SPRINGMVC @RequestBody receive JSON object strings
Previously, always thought in the SPRINGMVC environment, @RequestBody received a JSON object, has been debugging the code is not successful, and later found that, in fact, @RequestBody receive a JSON object string, rather than a JSON object. In AJAX requests, however, the JSON object is often passed, and it is later discovered that the object can be converted to a string using the Json.stringify (data) method. At the same time Ajax request also specify datatype: "JSON", ContentType: "Application/json" so you can easily upload an object or list to the Java side, You can bind an object or list using @requestbody.
JS Code:
<script type= "Text/javascript" >$ (document). Ready (function(){ varsavedataary=[]; vardata1={"UserName": "Test", "Address": "GZ"}; vardata2={"UserName": "Ququ", "Address": "GR"}; Savedataary.push (DATA1); Savedataary.push (DATA2); $.ajax ({type:"POST", URL:"User/saveuser", DataType:"JSON", ContentType:"Application/json", Data:JSON.stringify (SaveData), success:function(data) {}}); });
Java code:
@RequestMapping (value = "Saveuser", method = {requestmethod.post}}) @ResponseBody public void saveuser (@RequestBody list<user> users) { userservice.batchsave (users); }
SPRINGMVC @RequestBody receive JSON object strings