Spring Boot 配置错误页面

使用Spring Boot构建的WEB应用可以很方便的打成jar包发布,也可以打成war包发布到应用服务器中。自定义错误页面在这两种发布方式下是不一样的。

jar包中自定义错误页面

创建Spring Boot项目,默认打包方式是jar,内部使用内嵌tomcat等servlet容器

最简单的方式是直接在resources/templates目录下创建error.html页面,此时如果访问不存在的画面就会直接进入此画面。

404.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>错误页面</title>
</head>
<body>
<div class="error">
    <img alt="404" th:src="@{/public/images/404.gif}" style="max-width:800px; max-height:650px;">
    <div class="two" style="float:right;margin-right:300px;margin-top:8%;">
        <h2>很抱歉,页面它不小心迷路了 !</h2>
        <p style="color: rgb(144, 147, 153);font-size: 14px;">请检查您输入的网址是否正确,请点击以下按钮返回主页或者发送错误报告</p>
        <button type="button" class="layui-btn layui-btn-normal layui-btn-radius" style="margin-top:20px;" onclick="history.go(-1);return true;">返回首页</button>
    </div>
</div>
</body>
</html>

500.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>500</title>
</head>
<body>
    <div id="wrapper">
        <a class="logo" href="/"></a>
        <div id="main">
            <div id="header">
                <h1><span class="icon">!</span>500<span class="sub">Internal Server Error</span></h1>
            </div>
            <div id="content">
                <h2>服务器内部错误!</h2>
                <p>当您看到这个页面,表示服务器内部错误,此网站可能遇到技术问题,无法执行您的请求,请稍后重试或联系站长进行处理,医疗系统站长感谢您的支持!</p>
                <div class="utilities">
                    <div class="input-container" style="font: 13px 'TeXGyreScholaRegular', Arial, sans-serif;color: #696969; text-shadow: 0 1px white;text-decoration: none;">
                        <span id="totalSecond" style="color:red">5</span>秒后自动跳转…
                    </div>
                    <a class="button right" href="#" onClick="parent.location.reload();">返回首页...</a>
                    <a class="button right" href="#">联系站长</a>
                    <div class="clear"></div>
                </div>
            </div>

        </div>
    </div>
</body>
</html>

 二是实现ErrorPageRegistrar接口,定义具体异常的URL路径:

@Configuration
public class ErrorPageConfig implements ErrorPageRegistrar {

    @Override
    public void registerErrorPages(ErrorPageRegistry registry) {
        /*1、按错误的类型显示错误的网页*/
        /*错误类型为404,找不到网页的,默认显示404.html网页*/
        ErrorPage e404 = new ErrorPage(HttpStatus.NOT_FOUND, "/other/404");
        /*错误类型为500,表示服务器响应错误,默认显示500.html网页*/
        ErrorPage e500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/other/500");
        ErrorPage e400 = new ErrorPage(HttpStatus.BAD_REQUEST, "/other/500");
        registry.addErrorPages(e400 ,e404, e500);
    }

}

在一个配置类中声明该Bean:

    @Bean
    public ErrorPageRegistrar errorPageRegistrar(){
        return new MyErrorPageRegistrar();
    }

在Controller中添加404和500:

    /**
     * 404 error
     * @return
     */
    @RequestMapping("/404")
    public String error404() {
        return "commons/404";
    }

    /**
     * 500 error
     * @return
     */
    @RequestMapping("/500")
    public String error500() {
        return "commons/500";
    }

在resources/templaes/commons目录下创建404.html和500.html下即可。

参考链接:https://www.jianshu.com/p/7c94d1ac2092
posted @ 2020-09-03 18:05  QianTM  阅读(823)  评论(0编辑  收藏  举报