08_Spring_240517(금)_84일차(0) - 에러 처리

soowagger·2024년 5월 20일

8_Spring

목록 보기
34/38

⭐ HTTP 응답 상태 코드

400 : 잘못된 요청(Bad Request)
403 : 서버에서 외부 접근 거부(Forbidden)
404 : 요청 주소를 찾을 수 없다(Not Found)
405 : 허용되지 않은 메서드(요청방식) (Method Not Allowed)
500 : 서버 내부 오류(Internal Server Error)

common 예외 처리 html 및 컨트롤러 생성

/*
 * 스프링 예외 처리 방법(우선 순위별로 작성) 
 * 
 * 1. 메서드에서 직접 처리(try-catch, throws)
 * 
 * 2. 컨트롤러 클래스에서 클래스 단위로 모아서 처리
 * ( @ExceptionHandler 어노테이션을 지닌 메서드를 작성 )
 * 
 * 3. 별도 클래스를 만들어 프로젝트 단위로 모아서 처리
 * ( @ControllerAdvice 어노테이션을 지닌 클래스를 작성 )
 * 
 */

// @ControllerAdvice
// - 전역적 예외 처리

@ControllerAdvice
public class ExceptionController {
	
	// @ExceptionHandler(예외 종류)
	
	// 예외 종류 : 메서드별로 처리할 예외를 지정
	//      ex) @ExceptionHandler(SQLException.class) - SQL 관련 예외만 처리
	//          @ExceptionHandler(IOException.class) - 입출력 관련 예외만 처리
	//          @ExceptionHandler(Exception.class) - 모든 예외 처리
	
	@ExceptionHandler(NoResourceFoundException.class)
	public String notFound() {
		return "error/404";
	}
	
	
	// 예외 처리하는 메서드에서 사용가능한 매개변수 ( Controller에서 사용하는 모든 매개변수 작성 가능 )
	
	// 프로젝트에서 발생하는 모든 종류의 예외를 처리
	@ExceptionHandler(Exception.class)
	public String allExceptionHandler(Exception e, Model model) {
		
		e.printStackTrace();
		model.addAttribute("e", e);
		
		return "error/500";
	}
}

📌 404 : 요청 주소를 찾을 수 없다(Not Found)

📌 500 : 서버 내부 오류(Internal Server Error)

profile

0개의 댓글