SpringBoot配置自定义404,500错误页面
第一步 创建一个错误注册类ErrorPageRegister,继承 ErrorPageRegistrar,代码如下:
package com.bowen.service.config;import org.springframework.boot.web.server.ErrorPage;import org.springframework.boot.web.server.ErrorPageRegistry;import org.springframework.http.HttpStatus;import org.springframework.boot.web.server.ErrorPageRegistrar;public class ErrorPageRegister implements ErrorPageRegistrar {@Overridepublic void registerErrorPages(ErrorPageRegistry errorPageRegistry) {ErrorPage page404 = new ErrorPage(HttpStatus.NOT_FOUND, "/404");ErrorPage page500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500");errorPageRegistry.addErrorPages(page404, page500);}}
第二步 配置类中声明你的错误注册类,代码如下
/*** 错误配置页* @return*/@Beanpublic ErrorPageRegister errorPageRegistrar(){return new ErrorPageRegister();}
第三步 创建错误页面的路由
/*** 404 error* @return*/@RequestMapping("/404")public String error404() {return "html/errors/404";}/*** 500 error* @return*/@RequestMapping("/500")public String error500() {return "html/errors/500";}
这样大功告成了