Spring에서 자주 사용하는 두 가지 어노테이션으로 @Controller와 @RestController가 있다. 기존 실습 때는 @Controller를 사용했고, 진행 중인 사이드 프로젝트에선 @RestController를 사용했다. 두 어노테이션의 차이를 알고 싶어 글을 작성하게 됐다.
@Controller
- 목적: View를 반환하기 위해 설계
- 배경: Spring MVC 패턴을 구현하기 위해
- 반환 값: View 이름(String)

- DispatcherServlet이 Client의 URL 형식인 요청 수신
- HandlerMapping이 적절한 Controller를 찾아 매핑
- 선택한 Controller에게 HandlerAdapter가 역할 위임
- Controller는 받은 요청을 Service단에서 처리하고 View Name을 반환
- DispaterServlet은 반환한 View name을 ViewResolver를 통해 실제 View 구현체를 찾아 반환
@RestController
- 목적: REST API를 쉽게 개발하기 위해 설계
- 반환 값: json/xml, ResponseEntity

- DispatcherServlet이 Client의 URL 형식인 요청 수신
- HandlerMapping이 적절한 RestController를 찾아 매핑
- 선택한 RestController에게 HandlerAdapter가 역할 위임
- RestController는 받은 요청을 Service단에서 처리하고 객체를 반환
- 4번에서 반환한 객체를 포함한 Response Entity가 HttpMessageConverter를 만나 json, xml 등의 형식으로 Serialize되어 반환
@RestController = @Controller + @ResponseBody이다!
@ResponseBody는 controller의 반환값을 HTTP Response body에 직접 쓰는 역할을 한다. @Controller와 @ResponseBody를 함께 사용하면 @RestController처럼 객체(데이터)를 직접 반환할 수 있다.
여기서 객체를 반환한다는 의미는 '반환한 객체를 HTTP Response의 Body에 쓴다'이다.
References
- https://mangkyu.tistory.com/49
[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에서 추가되었습니다. 근본적인 차이점은 @Contr
dncjf64.tistory.com
'Spring' 카테고리의 다른 글
| [Spring] 간단한 DTO ↔ Entity 변환에도 Mapper가 필요할까? (1) | 2025.08.19 |
|---|---|
| [Spring] @ExceptionHandler와 @ControllerAdvice에 관하여 (1) | 2025.07.14 |
| [Spring] SSR과 CSR에서 Session 데이터 접근 방식의 차이 (1) | 2025.06.08 |
| [Spring] @Component와 그 친구들(@Controller, @Service, @Repository, @Configuration) (0) | 2025.03.20 |