본문 바로가기
Spring Boot

[Spring] @Controller vs @RestController

by palbokdev 2021. 9. 17.
@Controller
public class RestClassName{

  @RequestMapping(value={"/uri"})
  @ResponseBody
  public ObjectResponse functionRestName(){
      //...
      return instance
   }
}
@RestController
public class RestClassName{

  @RequestMapping(value={"/uri"})
  public ObjectResponse functionRestName(){
      //...
      return instance
   }
}

 

두 코드의 차이처럼 @RestController는 @ResponseBody가 기본적으로 추가되며,

response를 JSON/XML로 자동 변환해준다.

 

@RestControllerAdvice도 마찬가지로 @ControllerAdvice에 @ResponseBody가 기본값으로 추가된 것을 의미한다.

 

ref : https://stackoverflow.com/questions/25242321/difference-between-spring-controller-and-restcontroller-annotation

 

Difference between spring @Controller and @RestController annotation

Difference between spring @Controller and @RestController annotation. Can @Controller annotation be used for both Web MVC and REST applications? If yes, how can we differentiate if it is Web MVC o...

stackoverflow.com

 

ref 2 : https://stackoverflow.com/questions/43124391/restcontrolleradvice-vs-controlleradvice

 

@RestControllerAdvice vs @ControllerAdvice

What are the major difference between @RestControllerAdvice and @ControllerAdvice ?? Is it we should always use @RestControllerAdvice for rest services and @ControllerAdvice MVC ?

stackoverflow.com