SpringBoot 錯誤定製頁面快速配置

2021-10-04 12:10:08 字數 1621 閱讀 9729

springboot自動配置了錯誤頁面,但可讀性不是很高,而為了頁面很友好,可讀性更高,我們可以通過自己定製錯誤頁面,得到一些自己想要的資料資訊,具體請看下面操作
建立乙個myerrorcontroller類繼承basicerrorcontroller

/**

1. 錯誤頁面自定義

*/public

class

myerrorcontroller

extends

basicerrorcontroller

/** * 重新爺爺類的geterrorattributes的方法

}

建立乙個錯誤配置類,務必修改其他引數

/**

1. 錯誤處理相關配置

*/@configuration

public

class

errorconfiguration

}

結果除錯

* 增強版controller實現錯誤定製 所有controller丟擲異常後都將執行

* 通過responsebody返回給頁面

;可以在靜態資源路徑下建立error檔案再建立錯誤頁面,錯誤頁面建立規則具體如下

(借鑑了其他博主的文章)

可以使用

4xx和5xx作為錯誤頁面的檔名

來匹配這種型別的,所有錯誤

查詢規則,精確優先

優先尋找,精確的狀態碼.html

如果,沒有找到,就返回4xx.html頁面

比如,尋找403錯誤頁面

沒有的話,返回4xx.html

結果除錯

SpringBoot定製錯誤頁面

1 有模板引擎的情況下,例如404錯誤,將會在thymeleaf的templates的error下尋找404.html,如果找不到再尋找4xx.html 所有4開頭的錯誤狀態碼如果找不到特定的html頁面將會指向4xx.html 頁面可以獲取到的資訊 timestamp 時間戳 status 狀態碼...

SpringBoot定製錯誤頁面和錯誤資料

直接丟擲異常,那麼瀏覽器是便會訪問錯誤頁面 public string error model model lang en charset utf 8 titletitle head 55555555555 5xx.html body html timestamp 時間戳 status 狀態碼 er...

springboot定製錯誤處理頁面

springboot錯誤頁面處理 結構如下 在template資料夾下新建error資料夾 在error新建4xx.html檔案和5xx.html檔案 2.新建hellocontroller 和 login.html hellocontroller類 login.html頁面 3.錯誤 timest...