天天看点

SpringBoot配置错误页

配置错误页面路径

@Configuration
public class ErrorPageConfig {

	@Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
        return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
            @Override
            public void customize(ConfigurableWebServerFactory factory) {
                ErrorPage errorPage404 = new ErrorPage(HttpStatus.NOT_FOUND, "Error404.do");
                ErrorPage errorPage403 = new ErrorPage(HttpStatus.FORBIDDEN, "Error403.do");
                ErrorPage errorPage500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "Error500.do");
                factory.addErrorPages(errorPage404,errorPage403,errorPage500);
            }
        };
    }
}
           

错误页面路径映射

@Controller

public class ErrorController {

@RequestMapping(value = "/Error{errorCode}")
public String error(@PathVariable String branch) {
    logger.error("[email protected] {} !",branch);
    return "error/" + branch;
}
           

}

继续阅读