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(annotations = Controller.class)
public class MyControllerAdvisor {
@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);
String str2 = "o1o";
System.out.println(Integer.parseInt(str2));
int i = 1 / 0;
System.out.println(i);
...
...
@RequestMapping("zero")
public String zero() {
int i = 1 / 0;
System.out.println(i);
return "home";
}
@RequestMapping("format")
public String format() {
Integer.parseInt("zzz");
return "home";
}
npe.jsp
number.jsp
zero.jsp