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.
Asked
Active
Viewed 1,187 times
1

Tarion
- 16,283
- 13
- 71
- 107
1 Answers
3
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.

Oliver Drotbohm
- 80,157
- 18
- 225
- 211
-
1This issue is not clear at **Spring Data REST** [documentation](https://docs.spring.io/spring-data/rest/docs/current/reference/html/#customizing-sdr.overriding-sdr-response-handlers). In my case the overrided method controller returns _PagesResources>_ and without the _@ResponseBody_ annotation a **Circular view path exception** will be throw. It make me confused because the MVC annotation _@RestController_ include _@ResponseBody_ annotation by default on all methods and similar annotation _@RepositoryRestController_ not, why? – pdorgambide Aug 03 '17 at 06:21
-
But even when returning a ResponseEntity, it is not possible to return a plain String from a @BasePathAwareController :-( One can only return JSON. – Robert Dec 19 '18 at 08:07