728x90
반응형
@Controller
해당 어노테이션의 목적은 Model 객체를 만들어 데이터를 담고 view를 반환하기 위함이다.
아래와 같은 과정으로 요청과 응답이 진행된다.
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller

- 클라이언트에서 uri 형식을 통해 요청을 보낸다.
- DisPathcherServlet 에서 해당 요청을 받아 HandlerMapping 을 통해 위임할 Controller를 찾는다.
- Controller 에서는 비즈니스 로직을 진행하여 데이터를 받는다.
- HandlerAdapter는 View Name을 받아 DispatcherServlet 에게 전달하며 ViewResolver 를 통해 해당 하는 View 를 찾아 사용자에게 반환한다.
@RestController
@Controller + @ResponseBody 를 합친것과 같다.
해당 어노테이션은 Json 형태로 객체 데이터를 반환하기 위해 사용한다.
REST API 를 개발할 때 주로 사용하며, ResponseEntity로 감싸서 반환한다.
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController

- 클라이언트에서 uri 형식을 통해 요청을 보낸다.
- DisPathcherServlet 에서 해당 요청을 받아 HandlerMapping 을 통해 위임할 Controller를 찾는다.
- Controller 에서는 비즈니스 로직을 진행하여 데이터를 받는다.
- HandlerAdapter는 객체를 받아 HandlerAdapter 에게 반환한다.
- HandlerAdapter 내부에서 MassageConvert에 의해 변환되어 DispatchServlet에게 전달되고, 이후 사용자에게 반환한다.
참고
https://mangkyu.tistory.com/49?category=761302
[Spring] @Controller와 @RestController 차이
Spring에서 컨트롤러를 지정해주기 위한 어노테이션은 @Controller와 @RestController가 있습니다. 전통적인 Spring MVC의 컨트롤러인 @Controller와 Restuful 웹서비스의 컨트롤러인 @RestController의 주요한 차..
mangkyu.tistory.com
https://dncjf64.tistory.com/288
@Controller와 @RestController의 차이점
1.개요 Spring MVC의 @RestController은 @Controller와 @ResponseBody의 조합입니다. Spring 프레임 워크에서 RESTful 웹 서비스를 보다 쉽게 개발할 수 있도록 Spring 4.0에서 추가되었습니다. 근본적인 차이점은..
dncjf64.tistory.com
728x90
반응형
'Spring > Spring-detail' 카테고리의 다른 글
| 빈을 등록하기 위한 방법 @Configuraion, @Bean, @Component (0) | 2022.10.14 |
|---|---|
| DispatchServlet(디스패처 서블릿) 에 대해 알아보자. (0) | 2022.10.14 |
| @valid 를 사용해보자 (유효성 검사) (0) | 2022.10.13 |
| Swagger 적용하기 - REST API 명세를 문서화 하는 방법 (1) | 2022.10.08 |
| Integer 와 int 차이 (0) | 2022.10.05 |
댓글