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

Example code for deserializing into objects in spring mvc

零下一度
零下一度Original
2017-06-25 11:02:021432browse

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!

Statement:
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