The @Controller annotation marks a class as a Spring MVC web controller. It is a specialisation of @Component, so component scanning registers the class as a bean, and Spring MVC's handler mapping then looks inside it for methods annotated with @RequestMapping or its shortcuts such as @GetMapping and @PostMapping. Methods in a @Controller normally return a view name that a view resolver renders, for example a Thymeleaf template.
If you want to return JSON or plain text instead of HTML, use @RestController, which is @Controller plus @ResponseBody. The sections below show how both work in Spring Framework 6 and Spring Boot 3 and later.
@Controller does in Spring MVC@ResponseBody@Controller versus @RestController@Controller does in Spring MVC@Controller is a stereotype annotation. It is meta-annotated with @Component, which gives it two effects:
@SpringBootApplication) detects the class and registers it as a singleton bean.RequestMappingHandlerMapping treats a bean annotated with @Controller (or @RequestMapping at type level) as a source of request handlers and builds a mapping from URL, HTTP method and other conditions to each handler method.Using @Controller instead of a plain @Component is what signals the web layer role. It also keeps your architecture readable, because @Service and @Repository mark the other layers.
A handler method in a @Controller returns a logical view name by default. The DispatcherServlet passes that name to a ViewResolver, which locates the template and renders it with the data you placed in the Model.
The method puts a product attribute into the model and returns product/detail. With Thymeleaf on the classpath, Spring Boot resolves that name to src/main/resources/templates/product/detail.html. The controller never writes HTML itself, which keeps the view, the model and the controller separate.
@ResponseBodyAdd @ResponseBody to a method, or to the whole class, and the return value is no longer treated as a view name. Spring serialises it with an HttpMessageConverter, such as Jackson for JSON.
This returns {"status":"UP"} with the content type application/json. Mixing view-returning and body-returning methods in one @Controller is legal, which is useful for a page controller that also exposes a small AJAX endpoint.
@Controller versus @RestController@RestController is a convenience annotation that combines @Controller and @ResponseBody. Pick it for APIs where every method returns data.
| Aspect | @Controller | @RestController |
|---|---|---|
| Meta-annotations | @Component | @Controller, @ResponseBody |
| Default return handling | View name resolved by a ViewResolver | Serialised into the response body |
| Typical use | Server-rendered HTML pages | REST and JSON APIs |
Needs @ResponseBody for data | Yes, per method or class | No |
Once a class is a controller, its methods can declare many argument types and mapping rules.
@GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping map HTTP methods to handlers.@PathVariable, @RequestParam, @RequestHeader and @CookieValue bind parts of the request to arguments.@ModelAttribute binds form fields to an object, and @Valid triggers Bean Validation.@RequestBody converts the request body, for example JSON, into an object.RedirectAttributes and the redirect: prefix support the post-redirect-get pattern.On validation errors the method re-renders the form. On success it redirects, so a browser refresh does not submit the form twice.
Use @WebMvcTest to start only the web layer and MockMvc to send requests without a real server.
@MockitoBean is the Spring Framework 6.2 replacement for the older @MockBean from Spring Boot. On earlier versions, use @MockBean.
@Component instead of @Controller?A class annotated only with @Component is a bean, but Spring MVC will not detect its handler methods unless you also add @RequestMapping at class level. Use @Controller so the intent is explicit.
@Controller and @RestController?@RestController adds @ResponseBody to every method, so return values are written to the response body instead of being resolved as view names.
@Controller work with Spring WebFlux?Yes. The same annotation model is supported in WebFlux, but methods can return Mono and Flux and the stack is non-blocking.
In a @Controller, a returned String is a view name. Add @ResponseBody or switch to @RestController if you want the text itself.
@Controller is the stereotype that makes a class a Spring MVC web controller: it is registered by component scanning and its mapped methods handle requests. Return view names for server-rendered pages, add @ResponseBody or use @RestController for data, and test with @WebMvcTest and MockMvc.