SpringBoot 사용자 정의 오류 페이지

SpringBoot 에 서 는 SpringBoot 의 기본 오류 페이지 를 제외 하고 페이지 를 사용자 정의 할 수 있 습 니 다. 물론 의존 도 를 추가 해 야 합 니 다.
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-thymeleafartifactId>
        dependency>

Thymeleaf 를 제외 하고 Freemarker 의 의존 도 문제 가 없 을 것 입 니 다. 하지만 테스트 를 하지 않 았 습 니 다. 여기 서 Thymeleaf 를 예 로 들 겠 습 니 다.오류 페이지 를 사용자 정의 합 니 다. 우리 가 먼저 해 야 할 일 은 오류 페이지 를 설정 하 는 것 입 니 다. 여기 서 저 는 인터페이스 ErrorPageRegistrar 를 실현 하 는 방식 으로 오류 설정 을 합 니 다.
package com.boot.servlet.api.bootservlet.error;

import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.ErrorPageRegistrar;
import org.springframework.boot.web.server.ErrorPageRegistry;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

@Component
public class MyErrorPageRegistrar implements ErrorPageRegistrar {

    @Override
    public void registerErrorPages(ErrorPageRegistry registry) {
        ErrorPage errorPage404 = new ErrorPage(HttpStatus.NOT_FOUND, "/404");
        registry.addErrorPages(errorPage404);
    }
}

물론 ErrorPageRegistrar 를 설정 하 는 방식 으로 주입 할 수 있 습 니 다.
    @Bean
    public ErrorPageRegistrar errorPageRegistrar(){
        return new MyErrorPageRegistrar();
    }

위의 사용자 정의 오류 페이지 설정 에서 404 오 류 를 / 404 로 재 설정 하 였 습 니 다. 이 때 Controller 에 이 RequestMapping 경로 의 정의 만 있 으 면 사용자 정의 오류 페이지 로 성공 적 으로 이동 할 수 있 습 니 다.
package com.boot.servlet.api.bootservlet.controller;

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

@Controller
public class ErrorController {

    @GetMapping("/404")
    public String error404() {
        return "/error/404";
    }
}

주의해 야 할 것 은 여기 서 되 돌아 온 파일 /error/404resources 디 렉 터 리 에 저장 해 야 한 다 는 것 이다.

좋은 웹페이지 즐겨찾기