当前位置:网站首页>@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 注解的相关知识的总结,继续努力,加油~~~
边栏推荐
- If the development of the metauniverse still follows the development logic of the Internet, and its end point also follows
- Aggregate payment meets the needs of various industries to access a variety of payments
- Best practice cases of data security in the medical industry (desensitization version)
- Docker install MySQL 5.7
- 6.7 billion dollars! The acquisition of IDT by Renesas Electronics was finally approved!
- Beijing University of Posts and telecommunications | RIS assisted in-house multi robot communication system joint deep reinforcement learning
- Bond network card mode setting
- Behind Senma's LP, "Wenzhou Department" is running into the venture capital circle
- Apache Doris series: in depth understanding of real-time analytical database Apache Doris
- LVGL 8.2 Slider
猜你喜欢

Infinite connection · infinite collaboration | the first global enterprise communication cloud conference WECC is coming

Creativity: presentation of AI oil paintings with high imitation mineral pigments

What are the models of asemi from the manufacturer of rectifier bridge and how about the electroplating process of the manufacturer of rectifier bridge?

Deeply understand the connection state and reliable mechanism of TCP protocol

Spirng security (VIII) multiple filter chains coexist

ADS1256 debugging notes based on stm32hal Library

2019 telecast retest test questions

Xrrunner, a domestic performance testing tool for palm smart, officially unveiled qecon

Huawei cloud from entry to actual combat | cloud rapid site establishment service and enterprise host security service

Apipost signs up with Chinatelecom! Work together to accelerate the digital transformation of enterprises
随机推荐
Installation and tutorial of MATLAB curling simulation game
Infinite connection · infinite collaboration | the first global enterprise communication cloud conference WECC is coming
The United States has launched 337 investigations on a number of Chinese companies: Bubugao is full of lying guns!
Uniapp picker select the effect demo of provinces and cities (finishing)
DNS domain name resolution
Shell string
[internship] processing time
Huawei cloud from entry to actual combat | cloud rapid site establishment service and enterprise host security service
Custom dialog (including header and footer)
Sony announced the closure of Beijing mobile phone factory! The production line will be moved to Thailand, and the cost can be reduced by half!
[cloud picture theory] 247 first introduction to Huawei cloud analysis service
Implementing DDD based on ABP -- domain logic and application logic
Digital collections can go further without hype
Leetcode:733. image rendering [BFS]
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
Function and technical principle of data desensitization [detailed explanation]
Source code analysis of ArrayList
Day008 select structure (switch statement)
Apache Doris series: in depth understanding of real-time analytical database Apache Doris
Numpy overview