当前位置:网站首页>Detailed explanation of @RequestBody and @ResponseBody
Detailed explanation of @RequestBody and @ResponseBody
2022-07-30 10:56:00 【Whitemeen too white】
@ResponseBody 的作用其实是将 java 对象转为 json 格式的数据.
@responseBody 注解的作用是将 controller 的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到 response对象的 body 区,通常用来返回 JSON 数据或者是 XML 数据.
注意:在使用此注解之后不会再走视图处理器,而是直接将数据写入到输入流中,他的效果等同于通过 response 对象输出指定格式的数据.
@ResponseBody 是作用在方法上的,@ResponseBody 表示该方法的返回结果直接写入 HTTP response body 中,一般在异步获取数据时使用(也就是 AJAX).
**注意:**在使用 @RequestMapping 后,返回值通常解析为跳转路径,但是加上 @ResponseBody 后返回结果不会被解析为跳转路径,而是直接写入 HTTP response body 中. 比如异步获取 json 数据,加上 @ResponseBody 后,会直接返回 json 数据[email protected] 将 HTTP 请求正文插入方法中,使用适合的 HttpMessageConverter 将请求体写入某个对象.
后台 Controller类中对应的方法:
// 接收 http://localhost:8080/video/user/login?usename=admin&password=123456
@RequestMapping("/login")
@ResponseBody
public Object login(String name, String password, HttpSession session) {
user = userService.checkLogin(name, password);
session.setAttribute("user", user);
return new JsonResult(user);
}
如上面的登录后台代码可以改为:
// 接收 http://localhost:8080/video/user/login?usename=admin&password=123456
@RequestMapping("/login")
@ResponseBody
public Object login(@RequestBody User loginUuser, HttpSession session) {
user = userService.checkLogin(loginUser);
session.setAttribute("user", user);
return new JsonResult(user);
}
@RequestBody when acting on a parameter list,用于将前台发送过来固定格式的数据(xml 格式或者 json 等)封装为对应的 JavaBean 对象,
封装时使用到的一个对象是系统默认配置的,由 HttpMessageConverter 进行解析,然后封装到形参上.
边栏推荐
- 数据库事务,JDBC操作和数据类型
- WebAPI 复习
- 2022全球数字经济大会人工智能专场:AI安全受高度关注
- Quick Start Tutorial for flyway
- Re16: Read the paper ILDC for CJPE: Indian Legal Documents Corpus for Court Judgment Prediction and Explanation
- 【HMS core】【Analytics Kit】【FAQ】如何解决华为分析付费分析中付款金额显示为0的问题?
- Basemap和Seaborn
- If someone asks you about distributed transactions again, throw this to him
- 零代码开发入门:快速上手DIY函数公式的5个步骤
- Security tip: FreeType in Qt
猜你喜欢

Neural Ordinary Differential Equations

Paper reading: SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers

360 released a future-oriented EDR to protect the security of government and enterprise user terminals in an all-round way

Alibaba Cloud OSS Object Storage

typescript入门之helloworld

Quick Start Tutorial for flyway

PyQt5 - draw sine curve with pixels

Telerik2022 R2,有效的自动化测试

Re17: Read the paper Challenges for Information Extraction from Dialogue in Criminal Law

Do you really understand the 5 basic data structures of Redis?
随机推荐
鸿湖万联扬帆富设备开发板正式合入OpenHarmony主干
Pytorch中 nn.Transformer的使用详解与Transformer的黑盒讲解
Paper reading: SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers
OC-手动引用计数内存管理
MFCC转音频,效果不要太逗>V<!
ODrive应用 #4 配置参数&指令「建议收藏」
ABP学习资源整理
Meikle Studio-Look at Hongmeng Device Development Practical Notes 7-Network Application Development
【AGC】增长服务2-应用内消息示例
活动速递| Apache Doris 性能优化实战系列直播课程初公开,诚邀您来参加!
自适应控制——仿真实验一 用李雅普诺夫稳定性理论设计自适应规律
The configuration process and related syntax of writing markdown format notes in vscode
(***Key points***) Flink common memory problems and tuning guide (1)
vscode中写markdown格式笔记的配置过程和相关语法
typescript入门之helloworld
NLP领域的最新研究进展
RandLA-Net复现记录
Security思想项目总结
MFCC to audio, the effect should not be too funny >V
现在报PMP还来得及参加9月的考试吗?分享敏捷全真模拟题