Short version for the busy and the slightly dramatic
Think of @Controller as the traditional stage actor that hands off a scene to a view resolver and asks the template to do the heavy lifting. Think of @RestController as the actor who posts raw data to the feed and leaves the rendering drama to the client.
@Controller
public class PageController {
@GetMapping("/home")
public String home(Model model) {
model.addAttribute("name", "Guest");
return "home";
}
}
The controller example returns a string that maps to a template like home html which the view resolver finds and renders. This is server side rendering pure and simple.
@RestController
public class ApiController {
@GetMapping("/users")
public List list() {
return userService.findAll();
}
}
The RestController example returns a List of User objects which Spring converts to JSON using message converters. No view resolver involved unless you really try to make it sad.
When a method is annotated with @ResponseBody or the class is @RestController Spring skips view resolution and uses HttpMessageConverter instances to write the response. That means content negotiation for JSON XML or other formats works automatically based on Accept headers and available converters. Status codes headers and content type handling still work the same way so you can use ResponseEntity to control status and headers when you need to be explicit.
Final thought no magic here just intent. Use @Controller for pages and @RestController for APIs and your code will be easier to read debug and explain to the intern next quarter. Now go pick the one that matches your use case and stop arguing about annotations in meetings.
I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!
This is a dedicated watch page for a single video.