====== @Controller And @RestController Annotations ====== * description : @Controller And @RestController Annotations * author : 오션 * email : shlim@repia.com * lastupdate : 2022-12-13 Tue \\ ===== @Controller And @RestController Annotations ===== 오랫동안 스프링 프레임워크(Spring 2.5버전에서 도입)의 일부분을 담당해온 @Controller를 전통적인 Spring 컨트롤러로 사용할 수 있습니다.\\ Spring 4.0은 RESTful 웹 서비스 생성을 단순화하기 위해 @RestController 어노테이션을 도입했습니다.\\ @RestController 어노테이션은 @Controller와 @ResponseBody를 결합하는 편리한 어노테이션으로,\\ 컨트롤러 클래스의 메소드를 처리하는 모든 요청을 @ResponseBody 어노테이션으로 처리할 필요가 없습니다.\\ \\ ===== Spring MVC @Controller ===== @Controller 어노테이션으로 전통적인 컨트롤러를 표시할 수 있습니다.\\ 이 어노테이션은 단순히 @Component 클래스를 특화한 것으로, 클래스 패스 스캐닝을 통해 구현 클래스를 자동 감지할 수 있게 해줍니다.\\ \\ 메소드를 처리하는 요청을 위한 @RequestMapping 어노테이션과의 조합으로 @Controller를 일반적으로 사용합니다.\\ \\ Spring MVC controller 예제\\ @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) { // ... } } \\ @ResponseBody 어노테이션으로 메소드 처리 요청을 하고, HttpResponse로 반환 객체를 자동 직렬화할 수 있습니다.\\ ===== Spring MVC @RestController ===== @RestController는 컨트롤러를 특화한 버전입니다.\\ @RestController는 @Controller와 @ResponseBody 어노테이션을 포함하고 있으며, 컨트롤러 구현을 단순화하는 결과를 가져옵니다.\\ \\ @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) { // ... } } \\ == 상기 예제에서, 컨트롤러는 @RestController 어노테이션으로 처리되어 있기 때문에, @ResponseBody는 필요하지 않습니다.== 컨트롤러 클래스의 메소드를 처리하는 모든 요청은 자동으로 반환 객체를 HttpResponse로 직렬화합니다.\\ ===== Ref Link ===== [[https://www.baeldung.com/spring-controller-vs-restcontroller|The Spring @Controller and @RestController Annotations]]\\ [[https://docs.oracle.com/javaee/6/tutorial/doc/gijqy.html|Java EE6 Tutorial - What Are RESTful Web Services?]]\\ [[https://velog.io/@somday/RESTful-API-%EC%9D%B4%EB%9E%80|RESTful API 이란]]\\ [[https://velog.io/@soom/REST-Representational-State-Transfer|REST (Representational State Transfer)]]\\ [[https://www.geeksforgeeks.org/difference-between-controller-and-restcontroller-annotation-in-spring/|Difference Between @Controller and @RestController Annotation in Spring]]\\ {{tag> 오션 @Controller @RestController}}