How does the response handling differ between these two annotations?

@Controller and @RestController differ in how they handle the return value of a method — one treats it as a view name to render, the other serializes it directly into the HTTP response body.

Key Points: • A method in a @Controller returning a String is interpreted as a logical view name, resolved by a ViewResolver into HTML. • To send raw data like JSON from a @Controller, each method needs an explicit @ResponseBody annotation. • @RestController is a meta-annotation combining @Controller and @ResponseBody, so every method's return value is automatically serialized, typically to JSON via Jackson. • @Controller suits traditional server-rendered web applications; @RestController suits REST APIs consumed by frontends or other services. • Content negotiation still applies to @RestController responses — the client's Accept header can influence whether JSON or XML is returned.

Example: A @Controller method returning "home" triggers rendering of home.jsp, while a @RestController method returning a User object gets automatically converted to a JSON response body with no extra annotation needed.

Interview Tip: A concise interview answer is:

"With @Controller, a returned String is resolved as a view name, and you need @ResponseBody on any method that should return raw data instead. @RestController bakes @ResponseBody into every method automatically, so it's built for APIs that return JSON or XML rather than rendered web pages."