当前位置:网站首页>@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 进行解析,然后封装到形参上。
边栏推荐
- Adaptive Control - Simulation Experiment 1 Designing Adaptive Laws Using Lyapunov's Stability Theory
- TestNg整合Retry代码
- Re17: Read the paper Challenges for Information Extraction from Dialogue in Criminal Law
- (BUG record) No module named PIL
- flyway的快速入门教程
- [100个Solidity使用技巧]1、合约重入攻击
- Always remember: one day you will emerge from the chrysalis
- 在机器人行业的专业人士眼里,机器人行业目前的情况如何?
- Security思想项目总结
- 从数据流中快速查找中位数
猜你喜欢

typescript入门之helloworld

PyQt5 - draw sine curve with pixels

Meikle Studio-Look at Hongmeng Device Development Practical Notes 7-Network Application Development

Re16: Read the paper ILDC for CJPE: Indian Legal Documents Corpus for Court Judgment Prediction and Explanation

【云原生】-Docker安装部署分布式数据库 OceanBase

SST-Calib:结合语义和VO进行时空同步校准的lidar-visual外参标定方法(ITSC 2022)

论文阅读:SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers

第2章 常用安全工具

Selected System Design | Design of CAN Bus Controller Based on FPGA (with Code)
![MySQL installation tutorial [installation version]](/img/e9/9c7b0e3aac22206d126de428c1a4bd.png)
MySQL installation tutorial [installation version]
随机推荐
神秘的APT攻击
MySQL | Subqueries
Neural Network Study Notes 4 - Autoencoder (including sparse, stacked) (updated)
Basemap and Seaborn
阿里云OSS对象存储
Mysterious APT Attack
Array of Shell System Learning
Study Notes 10--Main Methods of Local Trajectory Generation
Flask之路由(app.route)详解
【C和指针第七章】可变参数列表
XYplorer 23多语言,最好的管理软件之一
再有人问你分布式事务,把这篇扔给他
SST-Calib: A lidar-visual extrinsic parameter calibration method combining semantics and VO for spatiotemporal synchronization calibration (ITSC 2022)
In 2022, the top will be accepted cca shut the list
Re15:读论文 LEVEN: A Large-Scale Chinese Legal Event Detection Dataset
MFCC转音频,效果不要太逗>V<!
Flink_CDC construction and simple use
Meikle Studio - see the actual combat notes of Hongmeng device development 4 - kernel development
(C language) file operation
4. yolov5-6.0 ERROR: AttributeError: 'Upsample' object has no attribute 'recompute_scale_factor' solution