当前位置:网站首页>@RequestBody 和 @ResponseBody 详解
@RequestBody 和 @ResponseBody 详解
2022-07-30 10:16:00 【Whitemeen太白】
@ResponseBody 的作用其实是将 java 对象转为 json 格式的数据。
@responseBody 注解的作用是将 controller 的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到 response对象的 body 区,通常用来返回 JSON 数据或者是 XML 数据。
注意:在使用此注解之后不会再走视图处理器,而是直接将数据写入到输入流中,他的效果等同于通过 response 对象输出指定格式的数据。
@ResponseBody 是作用在方法上的,@ResponseBody 表示该方法的返回结果直接写入 HTTP response body 中,一般在异步获取数据时使用(也就是 AJAX)。
**注意:**在使用 @RequestMapping 后,返回值通常解析为跳转路径,但是加上 @ResponseBody 后返回结果不会被解析为跳转路径,而是直接写入 HTTP response body 中。 比如异步获取 json 数据,加上 @ResponseBody 后,会直接返回 json 数据。@RequestBody 将 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 作用在形参列表上时,用于将前台发送过来固定格式的数据(xml 格式或者 json 等)封装为对应的 JavaBean 对象,
封装时使用到的一个对象是系统默认配置的,由 HttpMessageConverter 进行解析,然后封装到形参上。
边栏推荐
- 在机器人行业的专业人士眼里,机器人行业目前的情况如何?
- Verilog之数码管译码
- 2022全球数字经济大会人工智能专场:AI安全受高度关注
- 从数据流中快速查找中位数
- Paper reading: SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers
- 你真的懂Redis的5种基本数据结构吗?
- [AGC] Growth Service 2 - In-App Message Example
- 加密和安全
- 多线程保证单个线程开启事务并生效的方案
- [Qualcomm][Network] 网络拨号失败和netmgrd服务分析
猜你喜欢

If someone asks you about distributed transactions again, throw this to him

In the robot industry professionals, Mr Robot industry current situation?

【C和指针第七章】可变参数列表

Understanding of deadlock

第2章 常用安全工具

PyQt5 - Drawing different types of straight lines

Flink_CDC搭建及简单使用

【HMS core】【Analytics Kit】【FAQ】如何解决华为分析付费分析中付款金额显示为0的问题?

2022全球数字经济大会人工智能专场:AI安全受高度关注

Security Thought Project Summary
随机推荐
flowable workflow all business concepts
Domino Server SSL Certificate Installation Guide
BERT预训练模型系列总结
360 released a future-oriented EDR to protect the security of government and enterprise user terminals in an all-round way
关于verilog的时延研究
typescript入门之helloworld
Shell system learning function
从数据流中快速查找中位数
Meikle Studio-Look at the actual combat notes of Hongmeng device development six-wireless networking development
拖放事件,dataTransfer,getBoundingClientRect
MySQL | Subqueries
XYplorer 23多语言,最好的管理软件之一
Pytorch中 nn.Transformer的使用详解与Transformer的黑盒讲解
Telerik2022 R2,有效的自动化测试
多线程--线程和线程池的用法
Meikle Studio-Look at Hongmeng Device Development Practical Notes 7-Network Application Development
GNOME 新功能:安全启动被禁用时警告用户
【HMS core】【Analytics Kit】【FAQ】如何解决华为分析付费分析中付款金额显示为0的问题?
The thread pool method opens the thread -- the difference between submit() and execute()
Multithreading--the usage of threads and thread pools