问题描述
我正在使用 Spring-data-rest 并想知道 @RestController
也有 @ResponseBody
但 @RepositoryRestController
没有.
I'm using Spring-data-rest and wondering if there is a reason behind the fact that @RestController
also has @ResponseBody
but @RepositoryRestController
has not.
推荐答案
Spring Data REST 中使用该注解的所有控制器无论如何都会返回一个 ResponseEntity
,这样在技术上@ResponseBody
不是必需的.
All controllers in Spring Data REST using that annotation return a ResponseEntity<T>
anyway, so that technically @ResponseBody
is not needed.
我们通常更喜欢 ResponseEntity
作为返回类型有两个原因:
We generally prefer ResponseEntity
as return type for two reasons:
- 在处理 REST 请求的控制器方法中,您通常希望控制
ResponseEntity
正是其类型的响应的详细信息(标头、状态代码等). - Spring MVC 检测到
ResponseEntity
,因此我们不需要额外的注解.
- In controller methods serving REST requests, you usually want to control details of the response (headers, the status code etc.) for which
ResponseEntity
is exactly the type for. - Spring MVC detects
ResponseEntity
and thus we don't the additional annotation.
我不确定我们是否真的可以改变它,因为尽管注释的名称如此,但可能存在仍然使用视图分辨率的实现.如果您仍然认为这是一个好主意,请随时在我们的 JIRA 中提出请求.
I'm not sure we can actually change that, as despite the name of the annotation, there could be implementations out there that still use view resolution. If you still think, it's a good idea, feel free to raise a ticket in our JIRA.
这篇关于为什么@RepositoryRestController 没有像@RestController 这样的@ResponseBody 注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!