Reprinted from https://www.cnblogs.com/qiankun-site/p/5774325.html this blog
This article is only for self-organize knowledge
@ResponseBody
effect
- After the object is returned by the method of the controller is converted into the specified format by a suitable transducer, the response object is written to the region of the body, usually the data to return JSON or XML data. Note that, after this annotation processor would not go view, but directly writing data to the input stream, the same effect as the format specified by the output data target response.
Code demonstrates
Backstage
1 @ResponseBody 2 @RequestMapping("/update") 3 public Object update(User user) { 4 AJAXResult result = new AJAXResult(); 5 6 try { 7 userService.updateUser(user); 8 result.setSuccess(true); 9 } catch(Exception e) { 10 11 e.printStackTrace(); 12 result.setSuccess(false); 13 14 } 15 16 return result; 17 }
public class AJAXResult { private boolean success; private Object data; public Object getData() { return data; } public void setData(Object data) { this.data = data; } public boolean isSuccess() { return success; } public void setSuccess(boolean success) { this.success = success; } }
Reception received data: { "data": null, "success": true}
Effect is equivalent to the following code:
response.getWriter.write(JSONObject.fromObject(obj).toString());