当前位置:网站首页>@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 注解的相关知识的总结,继续努力,加油~~~
边栏推荐
- Creativity: Modern Art anonymous oil painting AI works presentation
- Project launch process of software testing technology
- Wechat applet experiment case: simple idiom dictionary
- Wechat applet application development (I)
- [golang from introduction to practice] poker licensing game
- Large screen visual adaptation file
- Creativity: presentation of AI oil paintings with high imitation mineral pigments
- PHP Baidu qianqianhua installment API
- Unity3d learning note 9 - loading textures
- HTC new VR all-in-one machine vive focus plus release: price 5699 yuan!
猜你喜欢

01 create project warehouse

Typical data Lake application cases

Pandora IOT development board learning (RT thread) - Experiment 16 WiFi module experiment (learning notes)

Tiktok live broadcast brings goods to help the industry go to sea, and Fastdata observation industry boutique salon helps the ecological development

The LAF protocol elephant of defi 2.0 may be one of the few profit-making means in your bear market

Properties of trees
![[detailed tutorial] a thorough article on mongodb aggregation query](/img/81/1ac7afa778849b8a4b103107fd9cb6.png)
[detailed tutorial] a thorough article on mongodb aggregation query

CVPR 2022 | content aware text logo image generation method

Implementing DDD based on ABP -- domain logic and application logic

DOM event flow
随机推荐
Metinfo function public function getcity() error: XXX function no permission load!!!
LVGL 8.2 Textarea
LVGL 8.2 Spinbox
Roleapp focuses on Web3 and opens the door of Web3 with the key of "data can be held"
When the development of the meta universe begins to show more and more the style of the Internet, we need to be vigilant
You are only one SQL statement away from the tdengine Developer Conference!
Spire. Office for net 7.7.2 and news
Server status code
Visual studio 2019 installation and uninstallation problems
Postgraduate entrance examination experience
Beijing University of Posts and telecommunications | RIS assisted in-house multi robot communication system joint deep reinforcement learning
Apipost signs up with Chinatelecom! Work together to accelerate the digital transformation of enterprises
In the process of data migration from Oracle to polardb for PostgreSQL, what does data migration mean?
DNS resolution experiment
Debezium series: Show slave status view the possible different situations of master-slave delay
Leetcode:733. image rendering [BFS]
Preparation for Android development in big companies
Es- retrieve the selected field from the search
Method of setting document comments in idea (graphic version)
Eve-ng lab simulator Cisco, H3C test host alias