Spring Boot @RestControllerAdvice

과녁스·2021년 9월 27일
0

Spring

목록 보기
5/11
post-thumbnail

개요


컨트롤러에서 AOP를 사용하는 경우가 있다면 가장 흔하게 볼 수 있는 것이 예외처리이다. 예외처리를 편리하게 해주기 위하여 사용할 수 있는 어노테이션이 @RestControllerAdvcie 이다.

사용법


기본 사용

간단하게 모든 예외에 대한 처리 방법을 만들 경우 아래와 같이 만들 수 있다.

@RestControllerAdvice
public class ExceptionAdvice {

// 모든 예외에 대한 처리
  @ExceptionHandler(Exception.class)
  public String exceptionAdvice(Exception e) {
      return "Exception!!!!!!!!!!!!";
  }
}

특정 클래스

@RestControllerAdvice(annotations = Example.class)
public class ExceptionAdvice {
}

복수 개의 예외 처리

@ExceptionHandler(value = {NullPointerException.class, ArrayIndexOutOfBoundsException.class})
public String exampleExceptionAdvice(RuntimeException e) {
	return "exampleException Occurred!!!!!!!!!!!!!";
}
profile
ㅎㅅㅎ

0개의 댓글