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