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