SPRINGMVC Handling json-using Httpmessageconverter

Source: Internet
Author: User

One, example-file download : @RequestMapping("/testhttpmessageconverter" ) Public responseentity< byte[]> testhttpmessageconverter (@RequestBody String Requestbody,HttpSession session)  throws ioexception{System. Out. println (requestbody);           InputStream in = Session.getservletcontext (). getResourceAsStream ("/web-inf/files/abc.pdf" ); byte [] bytes = new byte[in.available ()];in.read (bytes);          httpheaders headers = new httpheaders ();Headers.add ( "Content-disposition", "Attachment;filename=a.pdf" );          httpstatus StatusCode = httpstatus. OK;           responseentity< byte[]> re = new responseentity< by Te []> (bytes, headers, statusCode); return re;     }
◇ when the controller processing method is used to @RequestBody/@ResponseBody or httpentity<t>/responseentity<t>,
Spring first selects the matching httpmessageconverter based on the request header or the Accept property of the response header.
In order to get the matching httpmessageconverter according to the filter of the parameter type or generic type, if the available httpmessageconverter is not found, the error will be
◇ @RequestBody and @ResponseBody do not need to appear in pairs

Second, get JSON data examples :
1. Page request:< a href = "Getemployeesforjson" id = " JSON ">testforjson </a>
2. Post request:< Span style= "Color:rgb (63,127,127)" >script  < Span style= "Color:rgb (127,0,127)" >type =  "Text/javascript"  src = "scripts /jquery-1.9.1.min.js "  ></ script > <script type= "Text/javascript"> $ ( function() {$ ( "#json"). Click ( function () { var url= this. href; var args={};$.post (Url,args, function(data) {                   For ( var i=0;i<data.length;i++) {                   alert ( "LastName:+data[i].lastname+", Department: " +data[i].department.depar Tmentname);                                           }              }); return false ;          });     });
</script>
III. Processing of requests           @ResponseBody @RequestMapping( "/getemployeesforjson")Public collection<employee> getemployees () { return EmployeeDAO . GetAll ();     }     
Note: To add Jackson-all-1.9.11.jar in the JRE environment
O (∩_∩) o~ processing JSON data is so convenient, just need to add an annotation @responsebody on the corresponding processing method.

SPRINGMVC Handling json-using Httpmessageconverter

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.