Skip to content

@ControllerAdvice vs @ExceptinHandler #151

@skarltjr

Description

@skarltjr

@ExceptinHandler

  • 특정 컨트롤러에 대해서만 동작하는 예외 핸들링을 위한 어노테이션이다.
  • 또한 기본적으로 bean으로 등록되는게 아니다. 어노테이션을 보면 알 수 있다.
  • 만약 @ControllerAdvice가 없다면 아래처럼 특정 컨트롤러 하위에서만 해당 예외를 처리할 수 있다.
  • 스크린샷 2023-02-25 오후 11 57 36

@ControllerAdvice

  • 기존에 특정 컨트롤러에 한정적이었던 @ExceptionHandler를 global controller에서 발생하는 예외를 핸들링할 수 있게 만들어주는 어노테이션이다.
  • 실제 어노테이션 주석을보면 알 수 있다
 * Specialization of {@link Component @Component} for classes that declare
 * {@link ExceptionHandler @ExceptionHandler}, {@link InitBinder @InitBinder}, or
 * {@link ModelAttribute @ModelAttribute} methods to be shared across
 * multiple {@code @Controller} classes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions