作者:BNDong
鏈接:www.cnblogs.com/bndong/p/10135370.html
在啟動應用時會發(fā)現(xiàn)在控制臺打印的日志中出現(xiàn)了兩個路徑為 {[/error]} 的訪問地址,當系統(tǒng)中發(fā)送異常錯誤時,Spring Boot 會根據請求方式分別跳轉到以 JSON 格式或以界面顯示的 /error 地址中顯示錯誤信息。
2018-12-18 09:36:24.627 INFO 19040 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped '{[/error]}' ...
2018-12-18 09:36:24.632 INFO 19040 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped '{[/error],produces=[text/html]}' ...
Spring Boot 基礎就不介紹了,推薦下這個實戰(zhàn)教程:
https://github.com/javastacks/spring-boot-best-practice
使用 AJAX 方式請求時返回的 JSON 格式錯誤信息。
{
'timestamp': '2018-12-18T01:50:51.196+0000',
'status': 404,
'error': 'Not Found',
'message': 'No handler found for GET /err404',
'path': '/err404'
}
使用瀏覽器請求時返回的錯誤信息界面。
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
fastjson 是 JSON 序列化依賴, spring-boot-starter-freemarker 是一個模板引擎,用于我們設置錯誤輸出模板。最新 Spring Boot 面試題整理好了,大家可以在Java面試庫小程序在線刷題。
# 出現(xiàn)錯誤時, 直接拋出異常(便于異常統(tǒng)一處理,否則捕獲不到404)
spring.mvc.throw-exception-if-no-handler-found=true
# 不要為工程中的資源文件建立映射
spring.resources.add-mappings=false
spring:
# 出現(xiàn)錯誤時, 直接拋出異常(便于異常統(tǒng)一處理,否則捕獲不到404)
mvc:
throw-exception-if-no-handler-found: true
# 不要為工程中的資源文件建立映射
resources:
add-mappings: false
Spring Boot 基礎就不介紹了,推薦下這個實戰(zhàn)教程:
https://github.com/javastacks/spring-boot-best-practice
/**
* 信息實體
*/
public class ExceptionEntity implements Serializable {
private static final long serialVersionUID = 1L;
private String message;
private int code;
private String error;
private String path;
@JSONField(format = 'yyyy-MM-dd hh:mm:ss')
private Date timestamp = new Date();
public static long getSerialVersionUID() {
return serialVersionUID;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
}
/**
* 自定義異常
*/
public class BasicException extends RuntimeException {
private static final long serialVersionUID = 1L;
private int code = 0;
public BasicException(int code, String message) {
super(message);
this.code = code;
}
public int getCode() {
return this.code;
}
}
/**
* 業(yè)務異常
*/
public class BusinessException extends BasicException {
private static final long serialVersionUID = 1L;
public BusinessException(int code, String message) {
super(code, message);
}
}
BasicException 繼承了 RuntimeException ,并在原有的 Message 基礎上增加了錯誤碼 code 的內容。而 BusinessException 則是在業(yè)務中具體使用的自定義異常類,起到了對不同的異常信息進行分類的作用。分享資料:Spring Boot 學習筆記。
關注Java技術??锤喔韶?/strong>