问题
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.
回答1:
All controllers in Spring Data REST using that annotation return a ResponseEntity<T>
anyway, so that technically @ResponseBody
is not needed.
We generally prefer ResponseEntity
as return type for two reasons:
- 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.
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.
来源:https://stackoverflow.com/questions/33712303/why-does-repositoryrestcontroller-do-not-have-responsebody-annotation-like-re