免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Spring Cloud 如何統(tǒng)一異常處理?寫得太好了!
userphoto

2022.06.16 江蘇

關注

作者: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 學習筆記。

新建 error.ftl 模板文件

使用瀏覽器請求時返回的錯誤信息界面。

示例代碼:https://github.com/BNDong/spring-cloud-examples/tree/master/spring-cloud-zuul/cloud-zuul

參考資料:






Spring Boot 定時任務開啟后,怎么自動停止?
工作 3 年的同事不知道如何回滾代碼!
23 種設計模式實戰(zhàn)(很全)
Spring Boot 保護敏感配置的 4 種方法!
面了個 5 年 Java,兩個線程數據交換都不會
阿里為什么推薦使用 LongAdder?
新來一個技術總監(jiān):禁止戴耳機寫代碼。。
重磅!Spring Boot 2.7 正式發(fā)布
Java 18 正式發(fā)布,finalize 被棄用。。
Spring Boot Admin 橫空出世!
Spring Boot 學習筆記,這個太全了!

關注Java技術??锤喔韶?/strong>



獲取 Spring Boot 實戰(zhàn)筆記!
本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服