Using annotation @responsebody in Springmvc, @RequestBody passing JSON data

Source: Internet
Author: User

In Springmvc, you can use the @requestbody and @responsebody two annotations to complete the conversion of request messages to object and object-to-response messages, respectively.

[Email protected]

The annotation is used to write the object returned by the controller's method to the body data area of the response object after the appropriate httpmessageconverter is converted to the specified format.

That is, you can convert the returned object (the collection of JavaBean with data, list or map) to JSON.

[Email protected]

This annotation is used to read the body portion of the request requests, parse using the httpmessageconverter of the system's default configuration, and then bind the corresponding data to the object to be returned.

3.HttpMessageConverter

4.jackson Pack

5.ajax use to pay attention to the

1. Json.stringify () and $.parsejson () implement a conversion between a string and a JSON object. Note that the data to be passed is a string.

2. To set contenttype: ' Application/json ' instead of using the default value: "Application/x-www-form-urlencoded".

  

JSP page

$(function(){              $("#btn"). Click (function(){                                    varData2 = {"id": +, "name": "tt", "passwd": "345"}; //alert (json.stringify (DATA2));$.ajax ({type:' Post ', URL:' Json.action ', Cache:false, Data:JSON.stringify (data2), ContentType:' Application/json ', DataType:' JSON ', Success:function(Result) {//result = $.parsejson (result);Alert (result[0].password);                      Alert (json.stringify (result));              },                                        }); });

Controller

    @RequestMapping (value= "/json.action", method=requestmethod.post)    @ResponseBody    public List<author> GetAuthor2 (@RequestBody user user) {        System.out.println (User.getname ());         = Authorservice.findauthor (3);         = Authorservice.findauthor (4);        ListNew arraylist<author>();        List.add (AUT1);        List.add (AUT2);         return list;    }

Using annotation @responsebody in Springmvc, @RequestBody passing JSON data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.