Difference between spring @Controller and @RestController annotation
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Spring MVC, @Controller and @RestController are both controller annotations, but they serve different default response models. @Controller is primarily for MVC endpoints that return views, while @RestController is for endpoints that return response bodies directly, usually as JSON.
What @Controller means
@Controller marks a class as a Spring MVC controller whose methods typically resolve to view names.
Here the return value is the name of a view template, not raw response data.
This is the right model for server-rendered pages with Thymeleaf, JSP, or another view technology.
What @RestController means
@RestController is effectively @Controller plus @ResponseBody applied by default to handler methods.
Spring serializes the return value into the HTTP response body through message converters.
That is why @RestController is the normal choice for JSON APIs.
The default-behavior difference
The main difference is not that one can handle requests and the other cannot. Both can handle requests. The difference is what Spring assumes you mean by the return value.
- with
@Controller, a returned string is usually treated as a view name - with
@RestController, a returned value is written to the response body
That default changes how concise your code can be and what kinds of endpoints the controller is meant to represent.
Mixing behaviors with @Controller
You can still return response bodies from a plain @Controller by adding @ResponseBody on individual methods.
This is useful when one controller legitimately mixes page rendering and API output, though many teams prefer to separate those concerns into different classes.
When to choose each one
Use @Controller when:
- you are rendering HTML views on the server
- your return values represent template names
- you are following an MVC page-based architecture
Use @RestController when:
- you are building REST-style endpoints
- you want JSON or XML responses written directly
- the controller exists to expose data rather than render templates
Why this matters in debugging
A very common confusion happens when someone returns a string from a @Controller expecting raw text, but Spring interprets it as a view name. Switching to @RestController, or adding @ResponseBody, changes the meaning of that return value.
This is why two controllers can look structurally similar in code while producing completely different runtime behavior from the exact same Java return type.
That single misunderstanding explains a surprising number of “why is Spring trying to resolve a template named ok” issues.
Common Pitfalls
A common mistake is using @Controller for an API and forgetting @ResponseBody, which causes return values to be treated as view names.
Another mistake is using @RestController for a server-rendered page flow where you actually wanted template rendering.
A third mistake is thinking the annotations define business logic differences when the real distinction is response handling semantics.
Summary
- '
@Controlleris aimed at MVC controllers that usually return views.' - '
@RestControlleris aimed at endpoints that return response bodies directly.' - '
@RestControlleracts like@Controllerplus@ResponseBodyby default.' - Use
@Controllerfor template rendering and@RestControllerfor JSON-style APIs. - The difference mainly affects how Spring interprets handler return values.

