버전 비교

  • 이 줄이 추가되었습니다.
  • 이 줄이 삭제되었습니다.
  • 서식이 변경되었습니다.

...

코드 블럭
languagejava
linenumberstrue
@RestControllerAdvice
public class ExceptionAdvice {

    @ExceptionHandler(value = NotFoundException.class)
    public ResponseEntity<CustomErrorResponse> handleGenericNotFoundException(NotFoundException e) {
        CustomErrorResponse error = new CustomErrorResponse("NOT_FOUND_ERROR", e.getMessage());
        error.setTimestamp(LocalDateTime.now());
        error.setStatus((HttpStatus.NOT_FOUND.value()));
        return new ResponseEntity<>(error, HttpStatus.NOT_FOUND);
    }   

}

이렇게 작성한 REST 컨트롤러용 Global Exception Handler는 예외 발생시 다음과 같이 메시지를 전달합니다.

코드 블럭
languagejs
linenumberstrue
{
  "errorCode": "NOT_FOUND_ERROR",
  "errorMsg": "Customer not found with id 1",
  "status": 404,
  "timestamp": "2019-12-26 11:45:59"
}