-
카테고리
-
세부 분야
백엔드
-
해결 여부
미해결
안녕하세요 선생님 Controllor와 RestController 에 대해서 궁금증이 생겨 댓글 남깁니다.
21.03.27 16:45 작성 조회수 152
0
안녕하세요 선생님 Controllor와 RestController 에 대해서 궁금증이 생겨 댓글 남깁니다.
질문을 장황하게 썼다가 가독성이 안 좋은것 같아서 짧게 정리해보겠습니다!
- @RestController 는 @Controller 에 @ResponseBody 가 붙은 것이다.
- @ResponseEntity 는 HTTP Header 와 같이 Body 가 아닌 다른 정보를 세밀하게 조작할 때 사용한다.
- 그럼 HTTP Header와 같은 세부 정보를 이용할 때는 @Controller + ResponseEntity 를 이용한다.
이게 맞나요?
아니면
- @RestController의 목적은 @Controlloer와 달리 View와 같은 정보를 반환하지 않고 REST 한 리소스를 반환하기 위함이다.
- ResponseEntity 를 사용하면 Body와 Header를 조작하는데, 해당 컨트롤러의 목적에 맞게 (REST API를 제공하는 ) @ResponseBody 어노테이션이 필요 없음에도 불구하고 @RestController 를 이용한다.
뭐가 맞을까요?
답변을 작성해보세요.
1
백기선
지식공유자2021.03.28
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/RestController.html
문서에 쓰여있다시피 해당 컨트롤러의 모든 메소드에 @ResponseBody를 붙이지 않도록 편의성을 돕기 위한 애노테이션입니다.
A convenience annotation that is itself annotated with @Controller and @ResponseBody.
Types that carry this annotation are treated as controllers where @RequestMapping methods assume @ResponseBody semantics by default.
말씀하신대로 ResponseEntity를 쓰면 @Controller든 @RestController든 상관없이 응답 본문을 만들고 헤더도 설정할 수 있는데요. 글쎄요. @RestController에서 모든 핸들러가 ResponseEntity 를 리턴하는게 아니라면 그런게 맞다 틀리다를 따지는게 무슨 의미가 있을까요?
답변 1