SpringBoot定制三種錯(cuò)誤頁(yè)面及錯(cuò)誤數(shù)據(jù)方法示例
我們知道 Spring Boot 已經(jīng)提供了一套默認(rèn)的異常處理機(jī)制,但是 Spring Boot 提供的默認(rèn)異常處理機(jī)制卻并不一定適合我們實(shí)際的業(yè)務(wù)場(chǎng)景,因此,我們通常會(huì)根據(jù)自身的需要對(duì) Spring Boot 全局異常進(jìn)行統(tǒng)一定制,例如定制錯(cuò)誤頁(yè)面,定制錯(cuò)誤數(shù)據(jù)等。
定制錯(cuò)誤頁(yè)面
我們可以通過(guò)以下 3 種方式定制 Spring Boot 錯(cuò)誤頁(yè)面:
- 自定義 error.html
- 自定義動(dòng)態(tài)錯(cuò)誤頁(yè)面
- 自定義靜態(tài)錯(cuò)誤頁(yè)面
自定義 error.html
我們可以直接在模板引擎文件夾(/resources/templates)下創(chuàng)建 error.html ,覆蓋 Spring Boot 默認(rèn)的錯(cuò)誤視圖頁(yè)面(Whitelabel Error Page)。
示例 1
1. 在 spring-boot-adminex 的模板引擎文件夾(classpath:/resources/templates)下,創(chuàng)建一個(gè) error.html,代碼如下。
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>自定義 error.html</title>
</head>
<body>
<h1>自定義 error.html</h1>
<p>status:<span th:text="${status}"></span></p>
<p>error:<span th:text="${error}"></span></p>
<p>timestamp:<span th:text="${timestamp}"></span></p>
<p>message:<span th:text="${message}"></span></p>
<p>path:<span th:text="${path}"></span></p>
</body>
</html>
2. 啟動(dòng) Spring Boot,在完成登陸跳轉(zhuǎn)到主頁(yè)后,使用瀏覽器地訪問(wèn)“http://localhost:8080/111”,結(jié)果如下圖。

圖1:自定義 error.html
由圖 1 可以看出,Spring Boot 使用了我們自定義的 error.html 覆蓋了默認(rèn)的錯(cuò)誤視圖頁(yè)面(Whitelabel Error Page)。
自定義動(dòng)態(tài)錯(cuò)誤頁(yè)面
如果 Sprng Boot 項(xiàng)目使用了模板引擎,當(dāng)程序發(fā)生異常時(shí),Spring Boot 的默認(rèn)錯(cuò)誤視圖解析器(DefaultErrorViewResolver)就會(huì)解析模板引擎文件夾(resources/templates/)下 error 目錄中的錯(cuò)誤視圖頁(yè)面。
精確匹配
我們可以根據(jù)錯(cuò)誤狀態(tài)碼(例如 404、500、400 等等)的不同,分別創(chuàng)建不同的動(dòng)態(tài)錯(cuò)誤頁(yè)面(例如 404.html、500.html、400.html 等等),并將它們存放在模板引擎文件夾下的 error 目錄中。當(dāng)發(fā)生異常時(shí),Spring Boot 會(huì)根據(jù)其錯(cuò)誤狀態(tài)碼精確匹配到對(duì)應(yīng)的錯(cuò)誤頁(yè)面上。
示例 2
1. 在 spring-boot-adminex 的模板引擎文件夾下 error 目錄中,創(chuàng)建一個(gè)名為 404.html 的錯(cuò)誤頁(yè)面,代碼如下。
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>自定義動(dòng)態(tài)錯(cuò)誤頁(yè)面 404.html</h1>
<p>status:<span th:text="${status}"></span></p>
<p>error:<span th:text="${error}"></span></p>
<p>timestamp:<span th:text="${timestamp}"></span></p>
<p>message:<span th:text="${message}"></span></p>
<p>path:<span th:text="${path}"></span></p>
</body>
</html>
2. 啟動(dòng) Spring Boot,在完成登陸跳轉(zhuǎn)到主頁(yè)后,在瀏覽器地址欄輸入“http://localhost:8080/111”,結(jié)果如下圖。

圖2:自定義動(dòng)態(tài)錯(cuò)誤頁(yè)面(精確匹配)
模糊匹配
我們還可以使用 4xx.html 和 5xx.html? 作為動(dòng)態(tài)錯(cuò)誤頁(yè)面的文件名,并將它們存放在模板引擎文件夾下的 error 目錄中,來(lái)模糊匹配對(duì)應(yīng)類型的所有錯(cuò)誤,例如 404、400 等錯(cuò)誤狀態(tài)碼以“4”開(kāi)頭的所有異常,都會(huì)解析到動(dòng)態(tài)錯(cuò)誤頁(yè)面 4xx.html 上。
示例 3
在 spring-boot-adminex 的模板引擎文件夾下 error 目錄中,創(chuàng)建一個(gè)名為 4xx.html 的錯(cuò)誤頁(yè)面,代碼如下。
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>自定義動(dòng)態(tài)錯(cuò)誤頁(yè)面 4xx.html</h1>
<p>status:<span th:text="${status}"></span></p>
<p>error:<span th:text="${error}"></span></p>
<p>timestamp:<span th:text="${timestamp}"></span></p>
<p>message:<span th:text="${message}"></span></p>
<p>path:<span th:text="${path}"></span></p>
</body>
</html>
2. 啟動(dòng) Spring Boot,在完成登陸跳轉(zhuǎn)到主頁(yè)后,使用瀏覽器訪問(wèn)“http://localhost:8080/111”,結(jié)果如下圖。

圖3:自定義動(dòng)態(tài)錯(cuò)誤頁(yè)面(模糊匹配)
自定義靜態(tài)錯(cuò)誤頁(yè)面
若 Sprng Boot 項(xiàng)目沒(méi)有使用模板引擎,當(dāng)程序發(fā)生異常時(shí),Spring Boot 的默認(rèn)錯(cuò)誤視圖解析器(DefaultErrorViewResolver)則會(huì)解析靜態(tài)資源文件夾下 error 目錄中的靜態(tài)錯(cuò)誤頁(yè)面。
精確匹配
我們可以根據(jù)錯(cuò)誤狀態(tài)碼(例如 404、500、400 等等)的不同,分別創(chuàng)建不同的靜態(tài)錯(cuò)誤頁(yè)面(例如 404.html、500.html、400.html 等等),并將它們存放在靜態(tài)資源文件夾下的 error 目錄中。當(dāng)發(fā)生異常時(shí),Spring Boot 會(huì)根據(jù)錯(cuò)誤狀態(tài)碼精確匹配到對(duì)應(yīng)的錯(cuò)誤頁(yè)面上。
示例 4
1. 在 spring-boot-adminex 的靜態(tài)資源文件夾 src/recources/static 下的 error 目錄中,創(chuàng)建一個(gè)名為 404.html 的靜態(tài)錯(cuò)誤頁(yè)面,代碼如下。
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>自定義靜態(tài)錯(cuò)誤頁(yè)面 404.html</h1>
<p>status:<span th:text="${status}"></span></p>
<p>error:<span th:text="${error}"></span></p>
<p>timestamp:<span th:text="${timestamp}"></span></p>
<p>message:<span th:text="${message}"></span></p>
<p>path:<span th:text="${path}"></span></p>
</body>
</html>
2. 啟動(dòng) Spring Boot,在完成登陸跳轉(zhuǎn)到主頁(yè)后,使用瀏覽器訪問(wèn)“http://localhost:8080/111”,結(jié)果如下圖。

圖3:自定義靜態(tài)錯(cuò)誤頁(yè)面(精確匹配)
由于該錯(cuò)誤頁(yè)為靜態(tài)頁(yè)面,無(wú)法識(shí)別 Thymeleaf 表達(dá)式,因此無(wú)法展示與錯(cuò)誤相關(guān)的錯(cuò)誤信息。
模糊匹配
我們還可以使用 4xx.html 和 5xx.html 作為靜態(tài)錯(cuò)誤頁(yè)面的文件名,并將它們存放在靜態(tài)資源文件夾下的 error 目錄中,來(lái)模糊匹配對(duì)應(yīng)類型的所有錯(cuò)誤,例如 404、400 等錯(cuò)誤狀態(tài)碼以“4”開(kāi)頭的所有錯(cuò)誤,都會(huì)解析到靜態(tài)錯(cuò)誤頁(yè)面 4xx.html 上。
示例 3
在 spring-boot-adminex 的模板引擎文件夾下的 error 目錄中,創(chuàng)建一個(gè)名為 4xx.html 的錯(cuò)誤頁(yè)面,代碼如下。
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>自定義靜態(tài)錯(cuò)誤頁(yè)面 4xx.html</h1>
<p>status:<span th:text="${status}"></span></p>
<p>error:<span th:text="${error}"></span></p>
<p>timestamp:<span th:text="${timestamp}"></span></p>
<p>message:<span th:text="${message}"></span></p>
<p>path:<span th:text="${path}"></span></p>
</body>
</html>
2. 啟動(dòng) Spring Boot,在完成登陸跳轉(zhuǎn)到主頁(yè)后,使用瀏覽器訪問(wèn)“http://localhost:8080/111”,結(jié)果如下圖。

圖3:自定義靜態(tài)錯(cuò)誤頁(yè)面(模糊匹配)
錯(cuò)誤頁(yè)面優(yōu)先級(jí)
以上 5 種方式均可以定制 Spring Boot 錯(cuò)誤頁(yè)面,且它們的優(yōu)先級(jí)順序?yàn)椋鹤远x動(dòng)態(tài)錯(cuò)誤頁(yè)面(精確匹配)>自定義靜態(tài)錯(cuò)誤頁(yè)面(精確匹配)>自定義動(dòng)態(tài)錯(cuò)誤頁(yè)面(模糊匹配)>自定義靜態(tài)錯(cuò)誤頁(yè)面(模糊匹配)>自定義 error.html。
當(dāng)遇到錯(cuò)誤時(shí),Spring Boot 會(huì)按照優(yōu)先級(jí)由高到低,依次查找解析錯(cuò)誤頁(yè),一旦找到可用的錯(cuò)誤頁(yè)面,則直接返回客戶端展示。
定制錯(cuò)誤數(shù)據(jù)
我們知道,Spring Boot 提供了一套默認(rèn)的異常處理機(jī)制,其主要流程如下:
- 發(fā)生異常時(shí),將請(qǐng)求轉(zhuǎn)發(fā)到“/error”,交由 BasicErrorController(Spring Boot 默認(rèn)的 Error 控制器) 進(jìn)行處理;
- BasicErrorController 根據(jù)客戶端的不同,自動(dòng)適配返回的響應(yīng)形式,瀏覽器客戶端返回錯(cuò)誤頁(yè)面,機(jī)器客戶端返回 JSON 數(shù)據(jù)。
- BasicErrorController 處理異常時(shí),會(huì)調(diào)用 DefaultErrorAttributes(默認(rèn)的錯(cuò)誤屬性處理工具) 的 getErrorAttributes() 方法獲取錯(cuò)誤數(shù)據(jù)。
我們還可以定制 Spring Boot 的錯(cuò)誤數(shù)據(jù),具體步驟如下。
- 自定義異常處理類,將請(qǐng)求轉(zhuǎn)發(fā)到 “/error”,交由 Spring Boot 底層(BasicErrorController)進(jìn)行處理,自動(dòng)適配瀏覽器客戶端和機(jī)器客戶端。
- 通過(guò)繼承 DefaultErrorAttributes 來(lái)定義一個(gè)錯(cuò)誤屬性處理工具,并在原來(lái)的基礎(chǔ)上添加自定義的錯(cuò)誤數(shù)據(jù)。
1. 自定義異常處理類
被 @ControllerAdvice 注解的類可以用來(lái)實(shí)現(xiàn)全局異常處理,這是 Spring MVC 中提供的功能,在 Spring Boot 中可以直接使用。
1)在 net.biancheng.net.exception 包內(nèi),創(chuàng)建一個(gè)名為 UserNotExistException 的異常類,代碼如下。
package net.biancheng.www.exception;
/**
* 自定義異常
*/
public class UserNotExistException extends RuntimeException {
public UserNotExistException() {
super("用戶不存在!");
}
}
2)在 IndexController 添加以下方法,觸發(fā) UserNotExistException 異常,代碼如下。
@Controller
public class IndexController {
......
@GetMapping(value = {"/testException"})
public String testException(String user) {
if ("user".equals(user)) {
throw new UserNotExistException();
}
//跳轉(zhuǎn)到登錄頁(yè) login.html
return "login";
}
}
3)在 net.biancheng.www.controller 中,創(chuàng)建一個(gè)名為 MyExceptionHandler 異常處理類,代碼如下。
package net.biancheng.www.controller;
import net.biancheng.www.exception.UserNotExistException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
@ControllerAdvice
public class MyExceptionHandler {
@ExceptionHandler(UserNotExistException.class)
public String handleException(Exception e, HttpServletRequest request) {
Map<String, Object> map = new HashMap<>();
//向 request 對(duì)象傳入錯(cuò)誤狀態(tài)碼
request.setAttribute("javax.servlet.error.status_code",500);
//根據(jù)當(dāng)前處理的異常,自定義的錯(cuò)誤數(shù)據(jù)
map.put("code", "user.notexist");
map.put("message", e.getMessage());
//將自定的錯(cuò)誤數(shù)據(jù)傳入 request 域中
request.setAttribute("ext",map);
return "forward:/error";
}
}
2. 自定義錯(cuò)誤屬性處理工具
1)在 net.biancheng.www.componet 包內(nèi),創(chuàng)建一個(gè)錯(cuò)誤屬性處理工具類 MyErrorAttributes(繼承 DefaultErrorAttributes ),通過(guò)該類我們便可以添加自定義的錯(cuò)誤數(shù)據(jù),代碼如下。
package net.biancheng.www.componet;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.WebRequest;
import java.util.Map;
//向容器中添加自定義的儲(chǔ)物屬性處理工具
@Component
public class MyErrorAttributes extends DefaultErrorAttributes {
@Override
public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
Map<String, Object> errorAttributes = super.getErrorAttributes(webRequest, options);
//添加自定義的錯(cuò)誤數(shù)據(jù)
errorAttributes.put("company", "www.biancheng.net");
//獲取 MyExceptionHandler 傳入 request 域中的錯(cuò)誤數(shù)據(jù)
Map ext = (Map) webRequest.getAttribute("ext", 0);
errorAttributes.put("ext", ext);
return errorAttributes;
}
}
2)在 templates/error 目錄下,創(chuàng)建動(dòng)態(tài)錯(cuò)誤頁(yè)面 5xx.html,代碼如下。
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>自定義 error.html</title>
</head>
<body>
<p>status:<span th:text="${status}"></span></p>
<p>error:<span th:text="${error}"></span></p>
<p>timestamp:<span th:text="${timestamp}"></span></p>
<p>message:<span th:text="${message}"></span></p>
<p>path:<span th:text="${path}"></span></p>
<!--取出定制的錯(cuò)誤信息-->
<h3>以下為定制錯(cuò)誤數(shù)據(jù):</h3>
<p>company:<span th:text="${company}"></span></p>
<p>code:<span th:text="${ext.code}"></span></p>
<p>path:<span th:text="${ext.message}"></span></p>
</body>
</html>
3)啟動(dòng) Spring Boot,訪問(wèn)“http://localhost:8080/testException?user=user”,結(jié)果如下圖。

圖4:定制錯(cuò)誤數(shù)據(jù)
注意:為了避免攔截器干擾,建議先將攔截器屏蔽掉。?
以上就是SpringBoot自定義三種錯(cuò)誤頁(yè)面及錯(cuò)誤數(shù)據(jù)方法示例的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot 自定義錯(cuò)誤頁(yè)面、錯(cuò)誤數(shù)據(jù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java中的static關(guān)鍵字修飾屬性和方法(推薦)
這篇文章主要介紹了Java中的static關(guān)鍵字修飾屬性和方法,包括哪些成員屬性可以被static修飾,靜態(tài)屬性的訪問(wèn)方法示例詳解,需要的朋友可以參考下2022-04-04
Spring+SpringMVC+MyBatis深入學(xué)習(xí)及搭建(三)之MyBatis全局配置文件解析
這篇文章主要介紹了Spring+SpringMVC+MyBatis深入學(xué)習(xí)及搭建(三)之MyBatis全局配置文件解析,需要的朋友可以參考下2017-05-05
Java實(shí)現(xiàn)Excel批量導(dǎo)入數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)Excel批量導(dǎo)入數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08
java中為何重寫(xiě)equals時(shí)必須重寫(xiě)hashCode方法詳解
這篇文章主要給大家介紹了關(guān)于java中為什么重寫(xiě)equals時(shí)必須重寫(xiě)hashCode方法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11

