Spring Boot

ExceptionHandler

상국이 2022. 2. 28. 18:51
728x90

스프링 @MVC에서 예외 처리 방법

@GetMapping("/exception")
public String exceptionExample(){
    throw new SampleException();
}

@ExceptionHandler(SampleException.class)
public AppError sampleError(SampleException exception){
    return AppError.builder()
            .message("App error")
            .reason("na do mola")
            .build();
}

 

결과

 

커스텀 에러 페이지

/static/error 밑에 html페이지 생성 > error code와 같으면 좋음

결과

728x90