스프링15_ControllerAdvice

charl hi·2022년 1월 28일
0

Spring

목록 보기
16/25
post-thumbnail

ControllerAdvice

  • 예외처리

✨MyControllerAdvisor

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;

//아래 패키지 하위의 친구들을 감시, 배열 형식으로 다수의 패키지 넣을 수 있다.
//@ControllerAdvice(basePackages = {"com.kh.app20", "패키지2..."})
//@ControllerAdvice(basePackages = "com.kh.app20")
@ControllerAdvice(annotations = Controller.class)
public class MyControllerAdvisor {

	//감시할 대상
//	@GetMapping("/member/join")
//	@ExceptionHandler(내가 처리할 예외 종류)
	/*
	@ExceptionHandler(NullPointerException.class)
	public String method01(NullPointerException e) {
		System.out.println("==============");
		e.printStackTrace();
		return "error/npe";
	}
	
	@ExceptionHandler(NumberFormatException.class)
	public String method02(NumberFormatException e) {
		System.out.println("================");
		e.printStackTrace();
		return "error/number";
	}
	
	@ExceptionHandler(ArithmeticException.class)
	public String method03(ArithmeticException e) {
		System.out.println("=================");
		e.printStackTrace();
		return "error/zero";
	}
	*/
	
	@ExceptionHandler(RuntimeException.class)
	public String method01(RuntimeException e) {
		System.out.println("런타임 에러");
		return "error/npe";
	}
	@ExceptionHandler(Exception.class)
	public String method01(Exception e) {
		System.out.println("아무튼 에러");
		return "error/npe";
	}
}


HomeController

		String str1 = null;
		str1.substring(0, 2);	//npe error
		
		String str2 = "o1o";
		System.out.println(Integer.parseInt(str2));	//nfe error
		
		int i = 1 / 0;
		System.out.println(i);	//ame error
...
...
	@RequestMapping("zero")
	public String zero() {
		int i = 1 / 0;
		System.out.println(i);	//ame error
		return "home";
	}
	
	@RequestMapping("format")
	public String format() {
		Integer.parseInt("zzz");
		return "home";
	}


npe.jsp

number.jsp

  • .../number

zero.jsp

  • .../zero

0개의 댓글