JavaIntermediate#spring

What is the purpose of @RestController vs @Controller in Spring MVC?

@Controller returns view names to be resolved by a view resolver (for server-rendered pages). @RestController combines @Controller and @ResponseBody, automatically serializing return values (usually to JSON) directly into the HTTP response body.

Example
@RestController
class ApiController {
  @GetMapping("/users")
  List<User> getUsers() { return userService.findAll(); } // serialized to JSON automatically
}

Related Questions

1
JavaAdvanced#spring

What is Spring AOP and what is it used for?

Open
2
JavaAdvanced#spring

What is @Transactional and how does it work in Spring?

Open
3
JavaBeginner#spring

What is the difference between application.properties and application.yml in Spring Boot?

Open