package com.bughz.forum.system.config; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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; /** * @author bughz * @date 2020/01/08 * describe: */ @Component public class ErrorPageConfig implements ErrorPageRegistrar { private static final Logger logger = LoggerFactory.getLogger(ErrorPageConfig.class); @Override public void registerErrorPages(ErrorPageRegistry errorPageRegistry) { //1、按错误的类型显示错误的网页 //错误类型为404,找不到网页的,默认显示404.html网页 ErrorPage e400 = new ErrorPage(HttpStatus.BAD_REQUEST, "/error/404"); ErrorPage e404 = new ErrorPage(HttpStatus.NOT_FOUND, "/error/404"); //错误类型为500,表示服务器响应错误,默认显示500.html网页 ErrorPage e500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500"); errorPageRegistry.addErrorPages(e400,e404, e500); .... 其他错误代码可以从HttpStatus中查看,后面紧跟的“/error/xxx”是对应的controller地址,此控制层不建议处理逻辑代码,避免再次出错,导致死循环 } }