Easy java spring

12. Difference between @RestController and @Controller internally?

Sample answer: "@RestController is @Controller plus @ResponseBody. With @Controller, a returned String is a view name. With @ResponseBody, the return value is written to the body by an HttpMessageConverter, for example Jackson to JSON."

What the docs say:

  • @RestController is "a convenience annotation that is itself annotated with @Controller and @ResponseBody."
  • Every method "inherits the type-level @ResponseBody annotation and, therefore, writes directly to the response body versus view resolution."
  • The return value is "serialized to the response body through an HttpMessageConverter", and a class-level @ResponseBody is inherited by all methods.
Sources (3)
  1. @RestController javadoc
  2. Annotated controllers: Declaration
  3. @ResponseBody