@RestControllerAdvice vs @ControllerAdvice

后端 未结 3 1844
孤独总比滥情好
孤独总比滥情好 2020-12-14 07:03
  • What are the major difference between @RestControllerAdvice and @ControllerAdvice ??
  • Is it we should always use @RestControllerAdvice for rest services and @C
3条回答
  •  天涯浪人
    2020-12-14 07:37

    Exception: A good REST API should handle the exception properly and send the proper response to the user. The user should not be rendered with any unhandled exception. A REST API developer will have two requirements related to error handling.

    1. Common place for Error handling
    2. Similar Error Response body with a proper HTTP status code across APIs

    @RestControllerAdvice is the combination of both @ControllerAdvice and @ResponseBody

    The @ControllerAdvice annotation was first introduced in Spring 3.2.

    We can use the @ControllerAdvice annotation for handling exceptions in the RESTful Services but we need to add @ResponseBody separately.

    Note:
    GlobalExceptionHandler was annotated with @ControllerAdvice, thus it is going to intercept exceptions from controllers accross the application.

提交回复
热议问题