3. 如何自定義錯誤響應
在spring boot無論是後端錯誤還是前端頁面的錯誤,任何的錯誤頁面都有相應的對應。在spring boot中是如何控制錯誤頁面的樣式的呢?
首先看與錯誤處理有關的自動配置類errormvcautoconfiguration
該配置工作之前有兩個前提條件:
該應用必須是乙個servlet-web
的應用
servlet和dispatcherservlet必須存在於classpath
@conditionalonclass()
繼續看原始碼發現,該配置類給容器中注入了四個元件分別是
defaulterrorattributes
basicerrorcontroller
errorpagecustomizer
preserveerrorcontrollertargetclasspostprocessor(目的是為errorcontroller bean的定義設定屬性preservetargetclass:true,從而確保errorcontroller bean如果有**物件的話**物件總是**在類上而不是介面上。)
乙個靜態的內部註解類
defaulterrorviewresolverconfiguration
該配置類原始碼,該配置類的內部其實是由defaulterrorviewresolver
注入進去的。
@configuration
static class defaulterrorviewresolverconfiguration
注入的前提是:1. dispatcherservlet存在,並且defaulterrorviewresolver不存在。
@bean
@conditionalonbean(dispatcherservlet.class)
@conditionalo****singbean
public defaulterrorviewresolver conventionerrorviewresolver()
}
defaulterrorviewresolver
為乙個預設的錯誤視**析類,作用是決定去哪兒對映對應的頁面。
其內部主要的方法的思路如下:
請求交由resolveerrorview
尋找錯誤檢視。
真正的頁面處理動作由下面的resolve
執行,具體是根據錯誤碼去尋找對應的頁面。
比如:如果返回錯誤碼為404,則對映error/404,如果提供了模板引擎可以解析這個頁面位址就用模板引擎解析;如果模板引擎不可用,則靜態資源資料夾下找errorviewname對應的頁面 error/404.html
在引擎不可用的時候,用resolveresource
來尋找頁面。
}//該方法真正的檢視對映方法。具體是根據錯誤碼去尋找對應的頁面。
//比如:如果返回錯誤碼為404,則對映error/404
//如果提供了模板引擎可以解析這個頁面位址就用模板引擎解析
private modelandview resolve(string viewname, mapmodel)
//如果模板引擎不可用,則靜態資源資料夾下找errorviewname對應的頁面 error/404.html
return resolveresource(errorviewname, model);
}//模板引擎不可用時,處理錯誤頁面
private modelandview resolveresource(string viewname, mapmodel)
} catch (exception ex)
} return null;
}核心思路:
當使用者沒有自己配置err@orattributes
時候,defaulterrorattributes
才會生效。
defaulterrorattributes
通過geterrorattributes
方法將對乙個的屬性新增進去。
主要的錯誤資訊@bean
@conditionalo****singbean(value = errorattributes.class, search = searchstrategy.current)
public defaulterrorattributes errorattributes()
//新增對乙個的錯誤屬性
@override
public mapgeterrorattributes(webrequest webrequest, boolean includestacktrace)
該元件的核心作用如下:
當使用者自己沒有配置errorcontroller
時,basicerrorcontroller
才會生效。
該controller
針對於瀏覽器和json格式的請求做不同樣式的處理
(1):產生html型別的資料;瀏覽器傳送的請求來到這個方法處理@bean
@conditionalo****singbean(value = errorcontroller.class, search = searchstrategy.current)
public basicerrorcontroller basicerrorcontroller(errorattributes errorattributes)
(2):產生json資料,其他客戶端來到這個方法處理;
配置錯誤頁面,同樣由內部類errorpagecustomizer
實現,該類實現了介面errorpageregistrar
,該介面實際上是當前servlet容器物件的的工廠。
系統出現錯誤以後來到error請求進行處理;
@bean
public errorpagecustomizer errorpagecustomizer()
errorpagecustomizer
的核心方法,為註冊器提供乙個錯誤頁面獲取方法,獲取對應的controller的訪問路徑
@override
public void registererrorpages(errorpageregistry errorpageregistry)
1)、如何定製錯誤的頁面;@value("$")
private string path = "/error";
1)、有模板引擎的情況下;error/狀態碼;
【將錯誤頁面命名為 錯誤狀態碼.html 放在模板引擎資料夾裡面的 error資料夾下】,發生此狀態碼的錯誤就會來到 對應的頁面;
我們可以使用4xx和5xx作為錯誤頁面的檔名來匹配這種型別的所有錯誤,精確優先(優先尋找精確的狀態碼.html);
2)、沒有模板引擎(模板引擎找不到這個錯誤頁面),靜態資源資料夾下找;頁面能獲取的資訊;
timestamp:時間戳
status:狀態碼
error:錯誤提示
exception:異常物件
message:異常訊息
errors:jsr303資料校驗的錯誤都在這裡
3)、以上都沒有錯誤頁面,就是預設來到springboot預設的錯誤提示頁面;
SpringBoot入門0x002 URL 對映
將某個http請求對映到某個方法上 這個註解可以加在某個controller或者某個方法上,如果加在controller上,則這個controller中的所有路由對映都將會加上這個字首 下面會有栗子 如果加在方法上,則沒有字首 下面也有栗子 首先編寫controller,controller頭部的 ...
python入門指南 Python 入門指南
python 入門指南 release 3.6.3 date dec 10,2017 python 是一門簡單易學且功能強大的程式語言。它擁有高效的高階資料結構,並且能夠用簡單而又高效的方式進行物件導向程式設計。python 優雅的語法和動態型別,再結合它的解釋性,使其在大多數平台的許多領域成為編寫...
Spring Boot 參考指南(目錄)
本節簡要概述了spring boot參考文件,它充當文件其餘部分的對映。如果你要開始使用spring boot,或者一般的 spring 請閱讀本節,它回答了 什麼?如何?和 為什麼?的問題,它包括關於spring boot的介紹,以及安裝說明,然後我們將介紹如何構建你的第乙個spring boot...