spring boot 已經(jīng)做了統(tǒng)一的異常處理,下面看看如何自定義處理異常
1.錯誤碼頁面映射
1.1靜態(tài)頁面
必須配置在 resources/static/error文件夾下,以錯誤碼命名
下面是404錯誤頁面內(nèi)容,當訪問一個不存在的鏈接的時候,定位到此頁
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<!doctype html> <html lang= "en" > <head> <meta charset= "utf-8" > <title>not found</title> </head> <body> <h1>sorry, not found</h1> <p> status code is 404 </p> </body> </html> |
顯示效果
1.2動態(tài)模板頁面(頁面存放位置亦不能更改,且只有4xx和5xx這兩個名稱)
示例(5xx.ftl)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<!doctype html> <html lang= "en" > <head> <meta charset= "utf-8" > <title>error</title> </head> <body> <h1>server error</h1> <p> <div style= "color:red;font-size:large" >error code: ${status}-${error}</div> <div style= "color:red;font-size:medium" >error msg: ${exception!},${message!}</div> <div> <div id= "trace" style= "color:grey;font-size:small;display: none;border: 1px solid black;padding: 10px;" >${trace!}</div> </div> </p> </body> </html> |
故意訪問一個拋異常的請求(/test2/throwex)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@controller @requestmapping ( "/test2" ) public class test2 { @requestmapping ( "" ) public string index(){ return "test" ; } @requestmapping ( "/throwex" ) @responsebody public string throwex(){ int a = 3 ; int b= 0 ; return string.valueof(a/b); } } |
頁面效果
如果使用ajax訪問這個錯誤請求,則返回json表示的錯誤信息
演示頁面(/test2 -> test.ftl)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
<!doctype html> <html> <head> <meta charset= "utf-8" /> <title>test</title> <script type= "text/javascript" src= "/js/jquery-2.0.3.min.js" ></script> </head> <body> <h1>test page</h1> <div> <button onclick= "doajax();" >請求異常測試</button> <div id= "msg" ></div> <textarea id= "errormsg" style= "color:red;width: 80%;height:300px" ></textarea> </div> <script type= "text/javascript" > var doajax = function () { $.ajax({ url: '/test2/throwex' , async: true , type: 'get' , data:{}, datatype: 'json' , success:function (data) { $( '#msg' ).text(data); }, error: function(xhr, textstatus, errorthrown) { // alert(xmlhttprequest.status); // alert(xmlhttprequest.readystate); // alert(textstatus); $( '#errormsg' ).text(xhr.responsetext); } }); }; </script> </body> </html> |
訪問test頁面,點擊按鈕,查看效果如下:
這里分別用網(wǎng)頁瀏覽器和ajax訪問出錯的接口得到不同的結果,一個輸出錯誤頁面,一個輸出錯誤json,這個是由請求的接收類型決定的。
2.自定義錯誤碼到頁面的映射
2.1 靜態(tài)頁面映射配置(定制嵌入容器的時候控制錯誤碼映射關系)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
@component public class customizationbean implements embeddedservletcontainercustomizer{ /** * 定制方法一:實現(xiàn)embeddedservletcontainercustomizer * @param container */ @override public void customize(configurableembeddedservletcontainer container) { //container.setport(9000); } /** * 定制方法二:注入embeddedservletcontainerfactory * @return */ @bean public embeddedservletcontainerfactory servletcontainer() { tomcatembeddedservletcontainerfactory factory = new tomcatembeddedservletcontainerfactory(); factory.setport( 9001 ); factory.setsessiontimeout( 10 , timeunit.minutes); //優(yōu)先級高于配置在/static/error文件夾里面的404.html頁面 factory.adderrorpages( new errorpage(httpstatus.not_found, "/error/403.html" )); return factory; } } |
2.2 動態(tài)頁面映射配置
1
2
3
4
5
6
7
8
9
|
@component public class myerrorviewresolver implements errorviewresolver { @override public modelandview resolveerrorview(httpservletrequest request, httpstatus status, map<string, object> model) { return new modelandview( "/error/error" ,model); } } |
這個自定義viewresolver將替代默認的頁面處理器defaulterrorviewresolver,上述配置將所有錯誤(不區(qū)分錯誤碼)都映射到了錯誤頁面error.ftl,進行統(tǒng)一管理。
新建error.ftl頁面渲染錯誤內(nèi)容
和5xx.ftl類似的配置
3.一些異常配置
#basicerrorcontroller的請求路徑(你可以遇到錯誤后跳轉的路徑,該路徑是一個basicerrorcontroller的請求路徑,映射到它errorhtml或者error方法,由請求接受類型決定,一個返回錯誤頁面,一個返回錯誤json)
#server.error.path=/error
#always include stacktrace(是否包含異常的堆棧信息,默認never,頁面無法獲取到${trace!}的值,可取值never,always,on_trace_param; on_trace_param意思是請求帶上參數(shù) ?trace=true)
server.error.include-stacktrace=always
4.springboot異常處理流程
controller->出錯跳轉->basicerrorcontroller->調用errorhtml方法->geterrorattributes(獲取錯誤屬性返回model)->丟給頁面處理器errorviewresolver
所以你可以定制controller/定制errorattributes/定制errorviewresolver等,實現(xiàn)更加復雜的異常處理
以上這篇淺談spring boot 1.5.4 異常控制就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務器之家。