In what scenarios would you use @RestController over @Controller?

@RestController is a Spring MVC stereotype annotation that combines @Controller and @ResponseBody, so every method's return value is written directly to the HTTP response body instead of being resolved to a view. @Controller, by contrast, expects methods to return a logical view name that gets rendered into HTML by a view resolver.

Key Points: • Use @RestController when building REST APIs that return JSON or XML payloads to browsers, mobile apps, or other services. • @RestController saves boilerplate because you don't need @ResponseBody on every handler method. • Use @Controller when the application renders server-side views such as JSP or Thymeleaf pages. • A @Controller method can still return JSON for a single endpoint by adding @ResponseBody explicitly. • Mixing both stereotypes in the same application is common: REST endpoints use @RestController, page-rendering endpoints use @Controller.

Example: A shopping site might use @RestController for a /api/products endpoint that returns a JSON list consumed by a JavaScript front end, while using @Controller for a /checkout endpoint that renders an HTML page with Thymeleaf.

Interview Tip: A concise interview answer is:

"I use @RestController for API endpoints that return data like JSON directly, since it bakes in @ResponseBody on every method. I use @Controller when the method needs to return a view name for server-side rendering, such as a JSP or Thymeleaf page."