Requirements: spring mvc receives JSON data submitted by ajax and deserializes it into an object. The code is as follows:
Front-end JS code:
//属性要与带转化的对象属性对应var param={name:'语文',price:16}; $.ajax({ url: "/book/adddata", type: "POST", dataType: 'json',//必需设定,后台@RequestBody会根据它做数据反序列化contentType:"application/json",//必需把JSON数据以字符串的格式提交 data:JSON.stringify(param), success: function (data) { alert('添加成功'); }, error: function (XMLHttpRequest, textStatus) { alert('添加失败'); } });
Backend JAVA code :
@RequestMapping(value="adddata") @ResponseBodypublic Protocol addData(Model model, @RequestBody Book book) { Book reData = bookService.add(book);return reData; }
<br>
The above is the detailed content of Example code for deserializing into objects in spring mvc. For more information, please follow other related articles on the PHP Chinese website!