1. 程式人生 > 實用技巧 >Spring Boot 配置錯誤頁面

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