SpringBoot全域性異常處理

2021-09-11 20:46:35 字數 1016 閱讀 5207

1.寫乙個exception的配置類,在上面加上@controlleradvice註解

2.可以寫乙個總的異常處理,也可以寫單個的異常處理比如你要捕獲nullpointerexception註解需要寫乙個方法

@exceptionhandler(value=nullpointerexception.class)

@responsebody

public map getnullpointerexception(nullpointerexception e , httpservletrequest request)

其他註解一樣  比如總異常只需要修改handler裡面的nullpointerexception.class 

為exception.class 

3.輸出異常了

(1)請求的介面列印

log.error("請求位址"+request.getrequesturi());
(2)引數列印

log.error("********************====請求的引數*************************");

enumerationenumeration = request.getparameternames();

if (!enumeration.hasmoreelements())

while (enumeration.hasmoreelements())

(3)報錯資訊列印

log.error("********************====報錯資訊*************************");

stacktraceelement error = e.getstacktrace();

for (stacktraceelement stacktraceelement : error)

小結:我對這個異常的了解是它相當於乙個***,在給前端返回資料時進行異常攔截

全域性異常處理 springBoot 全域性異常處理

先讚後看,月入百萬 springboot開發的web專案中,強調分層的概念,乙個完整的專案一般會劃分出controller層和service層。因此,為了 的可維護性,controller層 應該盡量簡潔,驗證一下引數,直接丟給service層處理即可 異常處理的方式無外乎兩種 在springboo...

springboot全域性異常捕獲

新專案中需要用到檔案上傳,有需要對上傳檔案大小進行限制,當檔案超過限制的時候,springboot框架會直接丟擲異常,不會進入你的方法中,當我們需要向前臺返回資訊的時候也無從返回,只能進行全域性捕獲檔案過大的異常,然後再返回資訊。controlleradvice public class mycon...

SpringBoot全域性異常處理

簡介通常在controller層需要去捕獲service層的異常,防止返回一些不友好的錯誤資訊到客戶端,但如果controller層每個方法都用模組化的try catch 去捕獲異常,會很難看也難維護,所以使用全域性異常比較方便 這方法是springboot封裝好了的,我們直接使用即可,普通的配置我...