[Spring MVC] @Controller 와 @RestController 차이
https://www.baeldung.com/spring-controller-vs-restcontroller
Spring 4.0은 RESTful 웹 서비스 생성을 단순화하기 위해 @RestController 주석을 도입했다.
@Controller 와 @ResponseBody를 결합한 편리한 주석으로 컨트롤러 클래스의 모든 요청 처리 메서드에
@ResponseBody 주석을 달 필요가 없다.
Spring MVC @Controller
simply a specialization of the @Component class, which allows us to auto-detect implementation classes through the classpath scanning.
typically use @Controller in combination with a @RequestMapping annotation for request handling methods.
@Controller
@RequestMapping("books")
public class SimpleBookController {
@GetMapping("/{id}", produces = "application/json")
public @ResponseBody Book getBook(@PathVariable int id) {
return findBookById(id);
}
private Book findBookById(int id) {
// ...
}
}
annotated the request handling method with @ResponseBody. This annotation enables automatic serialization of the return object into the HttpResponse.
스프링 MVC @RestController
@RestController is a specialized version of the controller. It includes the @Controller and @ResponseBody annotations, and as a result, simplifies the controller implementation
@RestController
@RequestMapping("books-rest")
public class SimpleBookRestController {
@GetMapping("/{id}", produces = "application/json")
public Book getBook(@PathVariable int id) {
return findBookById(id);
}
private Book findBookById(int id) {
// ...
}
}
The controller is annotated with the @RestController annotation; therefore, the @ResponseBody isn't required.