1. 스프링 @MVC 예외 처리 방법
- @ControllerAdvice
전역적으로 예외 처리
(@ControllerAdvice 어노테이션이 붙은 클래스를 만들고 안에다가 ExceptionHandler 정의) - @ExceptionHandler
특정 컨트롤러 안에서 발생하는 예외 처리 (아래 4.@ExceptionHandler 참고)
2. 스프링 부트가 제공하는 기본 예외 처리기
- BasicErrorController
HTML과 JSON 응답 지원 - 커스터마이징 방법
ErrorControll 구현
3. 커스텀 에러 페이지
- 에러가 발생했을 때 응답의 상태 값에 따라 다른 웹 페이지를 보여주고 싶은 경우
src/main/resources/static|template/error 하위에
404.html, 5xx.html 처럼 상태 코드값으로 파일 생성 - 좀 더 동적인 컨텐츠 뷰로 에러페이지를 만들려면 ErrorViewResolver 구현하면 됨
# src/main/resources/static/error/404.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>404</h1>
</body>
</html>
4. @ExceptionHandler
: 특정 컨트롤러 안에서 예외처리
# Controller
@Controller
public class SampleController {
@GetMapping("/hello")
public String hello() {
throw new SampleExcption();
}
@ExceptionHandler(SampleExcption.class)
public @ResponseBody AppError sampleError(SampleExcption e) {
AppError appError = new AppError();
appError.setMessage("error.app.key");
appError.setReason("IDK IDK IDK");
return appError;
}
}
# AppError
public class AppError {
private String message;
private String reason;
// getter, setter 생략
}
# SampleException
public class SampleExcption extends RuntimeException {
}
https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81%EB%B6%80%ED%8A%B8/dashboard
스프링 부트 개념과 활용 - 인프런 | 강의
스프링 부트의 원리 및 여러 기능을 코딩을 통해 쉽게 이해하고 보다 적극적으로 사용할 수 있는 방법을 학습합니다., - 강의 소개 | 인프런...
www.inflearn.com
'Java > Spring Boot' 카테고리의 다른 글
[Spring Boot] CORS (0) | 2022.04.03 |
---|---|
[Spring Boot] Spring HATEOAS (0) | 2022.04.03 |
[Spring Boot] index 페이지와 favicon (0) | 2022.04.03 |
[Spring Boot] 웹 JAR (0) | 2022.04.03 |
[Spring Boot] 정적 리소스 지원 (0) | 2022.04.03 |