当前位置:网站首页>@ResponseBody注解的总结
@ResponseBody注解的总结
2022-07-25 04:03:00 【华为云】
[email protected] 注解的作用
@ResponseBody 是spring中的注解,注解没有任何参数,@ResponseBody 注解的主要作用是将Controller的方法返回的对象,通过转换器转换为指定的格式之后,写入到HTTP response body中,通常用来返回JSON数据或者是xml数据。
[email protected]使用在哪里
@ResponseBody注解一般会使用在Controller的方法上;也可以使用在整个Controller上,代表整个Controller中的方法都会采用@ResponseBody的方式转换数据,如果作用在整个Controller上可以和@Controller注解合并成@RestController注解来使用。
@ResponseBody注解还会起到ajax请求结束标识符的功能,当ajax请求从流中拿到这个注解才会结束请求,继续向下进行程序的运行.
[email protected]注解的浅析
在方法上使用了@RequestMapping注解的时候,方法的返回值通常解析为跳转的路径,即需要跳转到指定的页面。当添加了 @ResponseBody 这个注解后, 则表明该方法的返回值直接写入到 HTTP Response Body 中。@ResponseBody可以标注任何对象,由Srping完成对象和协议的转换。
如果是入参是字符串则直接将字符串写到客户端,如果入参是是一个对象,此时会将对象转化为json字符串然后再写到客户端。这里需要注意的是,如果返回对象,按utf-8编码。如果返回String,默认按iso8859-1编码,页面可能出现乱码。因此在注解中我们可以手动修改编码格式,例如@RequestMapping(value="/cat/query",produces="text/html;charset=utf-8"),前面是请求的路径,后面是编码格式。
@ResponseBody 注解中让Controller控制层方法的返回值转化为json格式的字符串是通过HttpMessageConverter中的方法实现类的转换的。如果是入参是bean对象,会调用对象的getXXX()方法获取属性值并且以键值对的形式进行封装,进而转化为json字符串。如果入参是是map集合,则会采用get(key)方式获取value值,然后进行封装,然后封装到形参上。
以上就是@ResponseBody 注解的相关知识的总结,继续努力,加油~~~
边栏推荐
- Debezium series: when there are a large number of DML operations in the record source database, the debezium consumption data time lags behind the data generation time by several hours
- Oracle uses FY_ recover_ Data recover truncate deleted data
- Top101 [linked list] must be applied for interview
- Introduction to computing system hardware (common servers)
- 01 create project warehouse
- Maker concept design to adapt to popular education
- Redis official visualization tool, with high appearance value and powerful functions!
- The LAF protocol elephant of defi 2.0 may be one of the few profit-making means in your bear market
- Huawei cloud from entry to actual combat | cloud rapid site establishment service and enterprise host security service
- Yuntu says digital asset chain: your God of digital asset property protection
猜你喜欢

Installation and tutorial of MATLAB curling simulation game

Method of setting document comments in idea (graphic version)

Roleapp focuses on Web3 and opens the door of Web3 with the key of "data can be held"

ECCV 2022 | rethinking image blending for data enhancement in vision transformers

Cluster clock synchronization configuration

Wechat applet experiment case: simple idiom dictionary

Eve-ng lab simulator Cisco, H3C test host alias

EMQ Yingyun technology was successfully selected into the 2022 "cutting edge 100" list of Chinese entrepreneurs
![Function and technical principle of data desensitization [detailed explanation]](/img/bb/5890d8fd140982ea7b994942093cc7.png)
Function and technical principle of data desensitization [detailed explanation]

MySQL eight shares
随机推荐
Multithreading advanced Step2
Millet 100W fast charging, 50W wireless charging technology exposure! Oppo Shen Yiren responded: boring!
Kubesphere 3.3.0 offline installation tutorial
Math. Random, switch selection structure
Dig deep into data dividends, Intel and industry accelerate the implementation of digital economy
Serial adder / subtracter
LVGL 8.2 Message box
The application could not be installed: INSTALL_ FAILED_ USER_ RESTRICTED
How to test cookies
Niuke interview high frequency list (group 1) difficulty: simple & medium
Only list the data of the specified field GetData ($table, '*', $where, $order)
[golang from introduction to practice] poker licensing game
01 create project warehouse
Visual studio 2019 installation and uninstallation problems
Apache Doris series: in depth understanding of real-time analytical database Apache Doris
Network engineering case: integrated network design of CII company
Bubble mart's market value evaporated by HK $21billion in seven days, which can't be sold in China, and its future at sea is uncertain
Oracle uses FY_ recover_ Data recover truncate deleted data
Which stock exchange has the lowest commission? Is online account manager safe to open an account
[detailed tutorial] a thorough article on mongodb aggregation query