[스프링부트] 30. 예외 처리

백하림's avatar
Apr 10, 2025
[스프링부트] 30. 예외 처리
400
Bad Requset
클라이언트의 요청 구문이 잘못됨
401
Unauthorized
인증되지 않음
403
Forbidden
권한 없음
404
Not Found
클라이언트가 요청한 리소스가 서버에 없음

Exception vs ExceptionApi의 차이

구분
Exception 등 (일반 예외)
ExceptionApi 등 (API 예외)
용도
페이지 요청 (일반 HTML) 에서 사용
AJAX 요청 (비동기, JSON 응답) 에서 사용
처리 방식
ModelAndView 등을 사용해서 에러 페이지로 이동
@ResponseBody or ResponseEntity 사용해서 JSON으로 에러 응답
예시
회원가입 페이지에서 잘못된 요청 시
댓글 등록 시 AJAX 요청에서 잘못된 입력일 때

Exception400

package shop.mtcoding.blog._core.erorr.ex; public class Exception400 extends RuntimeException { public Exception400(String message) { super(message); } }

Exception401

package shop.mtcoding.blog._core.erorr.ex; public class Exception401 extends RuntimeException { public Exception401(String message) { super(message); } }

Exception403

package shop.mtcoding.blog._core.erorr.ex; public class Exception403 extends RuntimeException { public Exception403(String message) { super(message); } }

Exception404

package shop.mtcoding.blog._core.erorr.ex; public class Exception404 extends RuntimeException { public Exception404(String message) { super(message); } }

ExceptionApi400

package shop.mtcoding.blog._core.erorr.ex; public class ExceptionApi400 extends RuntimeException { public ExceptionApi400(String message) { super(message); } }

ExceptionApi401

package shop.mtcoding.blog._core.erorr.ex; public class ExceptionApi401 extends RuntimeException { public ExceptionApi401(String message) { super(message); } }

ExceptionApi403

package shop.mtcoding.blog._core.erorr.ex; public class ExceptionApi403 extends RuntimeException { public ExceptionApi403(String message) { super(message); } }

ExceptionApi404

package shop.mtcoding.blog._core.erorr.ex; public class ExceptionApi404 extends RuntimeException { public ExceptionApi404(String message) { super(message); } }

GlobalExceptionHandler

package shop.mtcoding.blog._core.erorr; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import shop.mtcoding.blog._core.erorr.ex.*; import shop.mtcoding.blog._core.util.Resp; // 이 어노테이션을 붙이면 모든 에러가 여기로 들어온다. 데이터를 리턴하는 애 @RestControllerAdvice public class GlobalExceptionHandler { // 400 : 잘못된 요청 @ExceptionHandler(Exception400.class) public String ex400(Exception400 e) { String html = """ <script> alert('${msg}'); history.back(); </script> """.replace("${msg}", e.getMessage()); return html; } @ExceptionHandler(ExceptionApi400.class) public Resp<?> exApi400(ExceptionApi400 e) { return Resp.fail(400, e.getMessage()); } // 401 : 인증 안 됨 @ExceptionHandler(Exception401.class) public String ex401(Exception401 e) { String html = """ <script> alert('${msg}'); location.href = "/login-form" </script> """.replace("${msg}", e.getMessage()); return html; } @ExceptionHandler(ExceptionApi401.class) public Resp<?> exApi401(ExceptionApi401 e) { return Resp.fail(401, e.getMessage()); } // 403 : 권한 없음 @ExceptionHandler(Exception403.class) public String ex403(Exception403 e) { String html = """ <script> alert('${msg}'); </script> """.replace("${msg}", e.getMessage()); return html; } @ExceptionHandler(ExceptionApi403.class) public Resp<?> exApi403(ExceptionApi403 e) { return Resp.fail(403, e.getMessage()); } // 404 : 자원을 찾을 수 없음 @ExceptionHandler(Exception404.class) public String ex404(Exception404 e) { String html = """ <script> alert('${msg}'); location.href = "/"; </script> """.replace("${msg}", e.getMessage()); return html; } @ExceptionHandler(ExceptionApi404.class) public Resp<?> exApi404(ExceptionApi404 e) { return Resp.fail(404, e.getMessage()); } @ExceptionHandler(Exception.class) public String exUnKnown(Exception e) { String html = """ <script> alert('${msg}'); history.back(); </script> """.replace("${msg}", "관리자에게 문의해주세요."); System.out.println("관리자님 보세요 : " + e.getMessage()); return html; } }
Share article

harimmon