|
|
|
@ -6,16 +6,33 @@ import org.springframework.boot.web.server.ErrorPageRegistry;
|
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 定义一个错误配置类,实现 ErrorPageRegistrar 接口
|
|
|
|
|
*/
|
|
|
|
|
@Component
|
|
|
|
|
public class ErrorConfigurar implements ErrorPageRegistrar {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* 注册错误页面的方法
|
|
|
|
|
* @param registry 错误页面注册表
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void registerErrorPages(ErrorPageRegistry registry) {
|
|
|
|
|
ErrorPage[] errorPages=new ErrorPage[2];
|
|
|
|
|
errorPages[0]=new ErrorPage(HttpStatus.NOT_FOUND,"/error404");
|
|
|
|
|
errorPages[1]=new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR,"/error500");
|
|
|
|
|
// 创建一个错误页面数组
|
|
|
|
|
ErrorPage[] errorPages = new ErrorPage[2];
|
|
|
|
|
|
|
|
|
|
// 创建一个表示 404 状态的错误页面,并指定其重定向路径
|
|
|
|
|
ErrorPage errorPage404 = new ErrorPage(HttpStatus.NOT_FOUND, "/error404");
|
|
|
|
|
|
|
|
|
|
// 创建一个表示 500 状态的错误页面,并指定其重定向路径
|
|
|
|
|
ErrorPage errorPage500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error500");
|
|
|
|
|
|
|
|
|
|
// 将两个错误页面添加到错误页面数组中
|
|
|
|
|
errorPages[0] = errorPage404;
|
|
|
|
|
errorPages[1] = errorPage500;
|
|
|
|
|
|
|
|
|
|
// 将错误页面数组添加到错误页面注册表中
|
|
|
|
|
registry.addErrorPages(errorPages);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|