@Controller and @RestController both mark a class as a Spring MVC web controller, but they differ in what a method's return value is assumed to mean by default.
Key Points: • @Controller assumes a returned String is a logical view name to be resolved and rendered, fitting traditional server-rendered applications. • To return raw data such as JSON from a @Controller, each method needs an explicit @ResponseBody annotation. • @RestController is a convenience annotation that combines @Controller and @ResponseBody at the class level. • Every method in a @RestController automatically has its return value serialized into the response body, typically as JSON via Jackson. • @RestController is the standard choice for building REST APIs, while @Controller remains appropriate for apps serving HTML views like JSP or Thymeleaf pages.
Example: A method in a @Controller returning "dashboard" causes Spring to render dashboard.jsp, whereas the same method in a @RestController returning a DTO would be serialized straight into a JSON response with no view resolution involved.
Interview Tip: A concise interview answer is:
"@Controller is for classes rendering views, where returning a String means a view name and JSON output needs an explicit @ResponseBody. @RestController bundles @ResponseBody in by default, so every method's return value is serialized directly as the response body, which is exactly what you want for a REST API."