springboot 自定義404、500錯誤提示頁面的實(shí)現(xiàn)
springboot 默認(rèn)的異常處理機(jī)制
springboot 默認(rèn)已經(jīng)提供了一套處理異常的機(jī)制。一旦程序中出現(xiàn)了異常 springboot 會向 /error 的 url 發(fā)送請求。在 springboot 中提供了一個名為 BasicErrorController 的類來處理 /error 請求,然后跳轉(zhuǎn)到默認(rèn)顯示異常的頁面來展示異常信息
使用模板引擎
在使用 thymeleaf 等模板引擎時,springboot 會自動到 src/main/resources/templates/error/,文件夾下尋找 404.html、500.html 的錯誤提示頁面
錯誤提示頁面的命名規(guī)則就是:錯誤碼.html,如 404 是 404.html,500 是 500.html
使用示例
創(chuàng)建 springboot 項(xiàng)目如下
404、500 錯誤提示頁面結(jié)構(gòu)如下

application.properties 項(xiàng)目配置文件
server.port=8080 #它的默認(rèn)值就是classpath:/templates/,源碼在ThymeleafProperties類中 spring.mvc.view.prefix=classpath:/templates/ #它的默認(rèn)值就是.html,源碼在ThymeleafProperties類中 spring.mvc.view.suffix=.html spring.thymeleaf.cache=false
404 頁面內(nèi)容如下
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>404</title>
<link rel="shortcut icon" type="image/x-icon" th:href="@{/img/favicon.ico}" rel="external nofollow" rel="external nofollow" />
<link rel="stylesheet" type="text/css" th:href="@{/css/404.css}" rel="external nofollow" />
</head>
<body>
<div id="banner" style="height: 600px;width: 600px;margin-left: 370px"></div>
</body>
</html>
500 頁面內(nèi)容如下
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>500</title>
<link rel="shortcut icon" type="image/x-icon" th:href="@{/img/favicon.ico}" rel="external nofollow" rel="external nofollow" />
<link rel="stylesheet" type="text/css" th:href="@{/css/500.css}" rel="external nofollow" />
</head>
<body>
<div id="banner" style="height: 600px;width: 600px;margin-left: 370px"></div>
</body>
</html>
controller 如下
@Controller
public class PageController {
// 跳轉(zhuǎn)到登錄頁
@GetMapping(path = "/toLogin")
public String toLogin() {
int code = 1/0;
return "login";
}
}
404.html 頁面測試
訪問不存在的接口:http://localhost:8080/aaaa,結(jié)果如下

500.html 頁面測試
訪問已存在的接口:http://localhost:8080/toLogin,結(jié)果如下

沒有使用模板引擎
如果沒有使用 thymeleaf 等模板引擎時,springboot 會到靜態(tài)資源文件夾尋找 404.htm、500.html的錯誤提示頁面,命名同上。springboot 中默認(rèn)的靜態(tài)資源路徑有 4 個,分別是
classpath:/METAINF/resources/classpath:/resources/classpath:/static/classpath:/public/
優(yōu)先級順序?yàn)椋?code>META-INF/resources > resources > static > public,以上 4 種路徑創(chuàng)建 error 文件夾,再創(chuàng)建 404、500 錯誤提示頁面如下

不用寫額外的映射器,就能直接請求到
到此這篇關(guān)于springboot 自定義404、500錯誤提示頁面的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)springboot 自定義錯誤頁面內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot(cloud)自動裝配bean找不到類型的問題
這篇文章主要介紹了SpringBoot(cloud)自動裝配bean找不到類型的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
IDEA中Maven依賴包無法下載或?qū)氲慕鉀Q方案(系統(tǒng)缺失文件導(dǎo)致)
在配置Maven環(huán)境時,可能會遇到各種報錯問題,首先確保Maven路徑配置正確,例如使用apache-maven-3.5.0版本,則需要在系統(tǒng)環(huán)境變量的Path中添加其bin目錄路徑,并上移優(yōu)先級,接下來,在Maven的conf目錄下修改settings.xml文件,將鏡像源改為阿里云2024-09-09
Mybatis之typeAlias配置的3種方式小結(jié)
這篇文章主要介紹了Mybatis之typeAlias配置的3種方式小結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
記一次用IDEA打開java項(xiàng)目后不能運(yùn)行的解決方法
這篇文章主要介紹了記一次用IDEA打開java項(xiàng)目后不能運(yùn)行的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Spring?Boot中@Import三種使用方式實(shí)例詳解
這篇文章主要介紹了Spring?Boot中@Import三種使用方式,主要有引入普通類,引入importSelector的實(shí)現(xiàn)類及引入importBeanDefinitionRegister的實(shí)現(xiàn)類,結(jié)合實(shí)例代碼給大家講解的非常詳細(xì),需要的朋友可以參考下2022-11-11
mybatis-plus 關(guān)于savebatch,saveorupdatebatch遇到的坑及解決辦法
本文主要介紹了mybatis-plus 關(guān)于savebatch,saveorupdatebatch遇到的坑及解決辦法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01

