Difficulty: Beginner
What is the difference between @Controller and @RestController, and what role does @ResponseBody play?
Spring MVC was originally designed for server-rendered web pages, where a controller returns the name of a view, such as a Thymeleaf or JSP template, and the framework renders HTML. Modern backends mostly serve JSON to React or mobile clients, so the controller returns data instead. These two styles are what @Controller and @RestController represent, and the difference is small but important.
With @Controller, the value returned from a handler method is treated as a logical view name. If your method returns the string "home", Spring's ViewResolver looks up home.html or home.jsp and renders it, usually with a Model you populate. If you want to return data instead from a @Controller, you must add @ResponseBody to the method. That annotation tells Spring to skip view resolution and write the returned object straight into the HTTP response body.
@RestController is simply a convenience meta-annotation: @Controller plus @ResponseBody applied to every method in the class. So every return value is serialised into the response body. It is like ordering a meal with a standard side included instead of asking for the side on every dish. If you accidentally annotate a REST endpoint with @Controller and forget @ResponseBody, you will see a confusing error or a 404 as Spring tries to find a template named after your JSON string, which is a classic bug worth mentioning.
How does an object become JSON? Through HttpMessageConverters. When a handler returns a Java object, Spring checks the Accept header of the request and picks a converter. With Jackson on the classpath, which the web starter provides, MappingJackson2HttpMessageConverter serialises the object to JSON. Strings are handled by StringHttpMessageConverter, and if the client asks for XML and you have the XML dependency, the XML converter kicks in. This is called content negotiation. The reverse happens for incoming requests with @RequestBody, where the Content-Type header decides how the body is deserialised.
When would you still use @Controller? When the application renders server-side templates, or you have a mixed controller where most methods return views and only a few return JSON with @ResponseBody. Some teams use @Controller for redirects and forwards as well, for example returning "redirect:/login". In a pure API service, @RestController is the standard.
One more probing point: a @RestController is still a singleton bean, so it must be stateless. Also, keep the controller thin. It should parse input, call a service, and shape the response, not contain business rules or database access. That separation makes testing with @WebMvcTest simple and keeps the code maintainable.
@Controller
class PageController {
@GetMapping("/home")
public String home(Model model) {
model.addAttribute("name", "Asha");
return "home"; // view name -> templates/home.html
}
@GetMapping("/home/data")
@ResponseBody // opt-in JSON for this method only
public Map<String, String> data() { return Map.of("name", "Asha"); }
}
@RestController // @Controller + @ResponseBody on all methods
@RequestMapping("/api/students")
class StudentApi {
@GetMapping("/{id}")
public StudentDto get(@PathVariable Long id) {
return new StudentDto(id, "Asha");
}
}
The RestController serialises the return value with Jackson via an HttpMessageConverter.
RestController, Controller, ResponseBody, HttpMessageConverter