...
코드 블럭 |
---|
language | java |
---|
linenumbers | true |
---|
|
@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는 예외 발생시 다음과 같이 메시지를 전달합니다.
코드 블럭 |
---|
language | js |
---|
linenumbers | true |
---|
|
{
"errorCode": "NOT_FOUND_ERROR",
"errorMsg": "Customer not found with id 1",
"status": 404,
"timestamp": "2019-12-26 11:45:59"
} |