Home > Java > javaTutorial > Example code for deserializing into objects in spring mvc

Example code for deserializing into objects in spring mvc

零下一度
Release: 2017-06-25 11:02:02
Original
1450 people have browsed it

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('添加失败');
    }
});
Copy after login

Backend JAVA code :

    @RequestMapping(value="adddata")
    @ResponseBodypublic  Protocol addData(Model model, @RequestBody Book book) {
        Book reData = bookService.add(book);return reData;
    }
Copy after login

<br>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template