package hello.hellospring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ExceptionController {
@RequestMapping("/ex")
public void main() throws Exception{
throw new Exception("예외 발생");
}
}
예외처리를 하기 위한 ExceptionController를 만들고 localhost:8080/ex로 요청을 보내면 에러가 발생한다.
controller
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ExceptionController {
@RequestMapping("/ex")
public String main() throws Exception{
try {
throw new Exception("예외 발생");
} catch(Exception e) {
return "error";
}
}
}
타임리프
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Error</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1 th:text="'에러 발생'" ></h1>
</body>
</html>
localhost:8080/ex로 요청하면 에러페이지가 뜬다
Exception method 만들어 코드 리팩토링 해보기
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ExceptionController {
@ExceptionHandler(Exception.class)
public String catcher(Exception e){
return "error";
}
@RequestMapping("/ex")
public String main() throws Exception{
throw new Exception("예외 발생");
}
@RequestMapping("/ex2")
public String main2() throws Exception{
throw new Exception("예외 발생");
}
}
이전 코드에서 try, catcher 블럭을 사용해서 예외처리를 했다면 리팩토링한 코드는 Exception method를 따로 만들어 예외처리를 했다.
NullPointerException 추가해서 리팩토링
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ExceptionController {
@ExceptionHandler(NullPointerException.class)
public String catcher2(Exception e){
return "error";
}
@ExceptionHandler(Exception.class)
public String catcher(Exception e){
return "error";
}
@RequestMapping("/ex")
public String main() throws Exception{
throw new Exception("예외 발생");
}
@RequestMapping("/ex2")
public String main2() throws Exception{
throw new NullPointerException("예외 발생");
}
}
뷰 템플릿에서 에러 출력하기
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ExceptionController {
@ExceptionHandler(NullPointerException.class)
public String catcher2(Exception error, Model model){
model.addAttribute("error", error);
return "error";
}
@ExceptionHandler(Exception.class)
public String catcher(Exception error, Model model){
model.addAttribute("error", error);
return "error";
}
@RequestMapping("/ex")
public String main() throws Exception{
throw new Exception("예외 발생");
}
@RequestMapping("/ex2")
public String main2() throws Exception{
throw new NullPointerException("예외 발생");
}
}
model에 error 객체 담아 전달
@ExceptionHandler(NullPointerException.class)
public String catcher2(Exception error, Model model){
model.addAttribute("error", error);
return "error";
}
@ExceptionHandler(Exception.class)
public String catcher(Exception error, Model model){
model.addAttribute("error", error);
return "error";
}
타임리프
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Error</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1 th:text="${error}" ></h1>
</body>
</html>
브라우저로 에러가 출력되는 것을 확인할 수 있다