dev/spring boot + intelliJ
ErrorPage 만들기
dev_0hoon
2023. 12. 1. 19:39
먼저 예제로 에러컨트롤러를 만든다.
package hello.exception.servlet;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import java.io.IOException;
@Slf4j
@Controller
public class ServletExController {
@GetMapping("/error-ex")
public void errorEx(){
throw new RuntimeException("예외 발생");
}
@GetMapping("/error-404")
public void error404(HttpServletResponse response) throws IOException {
response.sendError(404,"404 d오류!");
}
@GetMapping("/error-500")
public void error500(HttpServletResponse response) throws IOException {
response.sendError(500,"404 d오류!");
}
}
만약 이 url을 발생 시켯을 때에는 기본 에러 페이지가 나올 것이다. 이쁜 에러페이지를 위해 에러페이지컨트롤러와 에러페이지를 만들어 준다.
@RequestMapping("/error-page/404")
public String errorPage404(HttpServletResponse response, HttpServletRequest request){
log.info("errorPage 404");
printErrorInfo(request);
return "error-page/404";
}
@RequestMapping("/error-page/500")
public String errorPage500(HttpServletResponse response, HttpServletRequest request){
printErrorInfo(request);
log.info("errorPage 500");
return "error-page/500";
}

하지만 이대로는 ErrorPageController에 닿지 않는다.
package hello.exception;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
@Component
public class WebServerCustomizer implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {
@Override
public void customize(ConfigurableWebServerFactory factory) {
ErrorPage errorPage404 = new ErrorPage(HttpStatus.NOT_FOUND, "/error-page/404");
ErrorPage errorPage500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error-page/500");
ErrorPage errorPageEx = new ErrorPage(RuntimeException.class, "/error-page/500");
//런타임 또는 그 자식 타입의 에러일 경우 500 (500 예외가 서버내부에서 발생한 오류라는 뜻을 포함하고 있기 때문에 여기서는 예외가
//발생한 경우도 500 오류 화면으로 처리했다.
factory.addErrorPages(errorPage404,errorPage500,errorPageEx);
}
}
1. 위의 WebServerCustomizer 클래스를 만들어 준다.
2. WebServerFactoryCustomizer<ConfigurableWebServerFactory> 인터페이스를 상속받아 customize를 오버라이드 해준 뒤에
3. ErrorPage 객체에 에러에 따른 redirect url을 부여해준다.
4. factory 객체에 errorPage객체를 넣어준다.
이러면 리다이렉트와 똑같지 않은가? 싶을 수 있다.
먼저 최초의 오류가 날 경우 어플리케이션 -> was 로 에러를 보내주는데 그때 was에서 등록되어있는 에러페이지 url로 에러정보를 함께보내준다.
그때 위에 만들었던
package hello.exception.servlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Slf4j
@Controller
public class ErrorPageController {
//RequestDispatcher 상수로 정의되어 있음
public static final String ERROR_EXCEPTION = "jakarta.servlet.error.exception";
public static final String ERROR_EXCEPTION_TYPE = "jakarta.servlet.error.exception_type";
public static final String ERROR_MESSAGE = "jakarta.servlet.error.message";
public static final String ERROR_REQUEST_URI = "jakarta.servlet.error.request_uri";
public static final String ERROR_SERVLET_NAME = "jakarta.servlet.error.servlet_name";
public static final String ERROR_STATUS_CODE = "jakarta.servlet.error.status_code";
@RequestMapping("/error-page/404")
public String errorPage404(HttpServletResponse response, HttpServletRequest request){
log.info("errorPage 404");
printErrorInfo(request);
return "error-page/404";
}
@RequestMapping("/error-page/500")
public String errorPage500(HttpServletResponse response, HttpServletRequest request){
printErrorInfo(request);
log.info("errorPage 500");
return "error-page/500";
}
private void printErrorInfo(HttpServletRequest request){ //오류페이지가 뜰때 어떤 오류인지 확인을 위해 만들었다.
log.info("ERROR_EXCEPTION: {}", request.getAttribute(ERROR_EXCEPTION));
log.info("ERROR_EXCEPTION_TYPE: {}", request.getAttribute(ERROR_EXCEPTION_TYPE));
log.info("ERROR_MESSAGE: {}", request.getAttribute(ERROR_MESSAGE));
log.info("ERROR_REQUEST_URI: {}", request.getAttribute(ERROR_REQUEST_URI));
log.info("ERROR_SERVLET_NAME: {}", request.getAttribute(ERROR_SERVLET_NAME));
log.info("ERROR_STATUS_CODE: {}", request.getAttribute(ERROR_STATUS_CODE));
log.info("dispatchType={}",request.getDispatcherType());
}
}
이곳의 에러페이지 쪽에서 printErrorInfo를 보면 request의 Attribute에서 에러 내용을 꺼내 볼 수 있게 된다.