SpringBoot 全域性異常處理

2021-10-05 03:13:53 字數 4482 閱讀 8943

springboot 它提供了乙個@controlleradvice 的註解,使用該註解表示開啟了全域性異常的捕獲,我們只需在自定義乙個方法使用exceptionhandler 註解然後定義捕獲異常的型別即可對這些捕獲的異常進行統一的處理。

@controlleradvice

public

class

myexceptionhandler

}

上述的示例中,我們對捕獲的異常進行簡單的二次處理,返回異常的資訊,雖然這種能夠讓我們知道異常的原因,但是在很多的情況下來說,可能還是不夠人性化,不符合我們的要求。

那麼我們這裡可以通過自定義的異常類以及列舉類來實現我們想要的那種資料吧。

首先定義乙個基礎的介面類,自定義的錯誤描述列舉類需實現該介面。

public

inte***ce

baseerrorinfointe***ce

然後我們這裡在自定義乙個列舉類,並實現該介面。

public

enum commonenum implements

baseerrorinfointe***ce

@override

public string getresultcode()

@override

public string getresultmsg()

}

然後我們在來自定義乙個異常類,用於處理我們發生的業務異常。

public

class

bizexception

extends

runtimeexception

public

bizexception

(baseerrorinfointe***ce errorinfointe***ce)

public

bizexception

(baseerrorinfointe***ce errorinfointe***ce, throwable cause)

public

bizexception

(string errormsg)

public

bizexception

(string errorcode, string errormsg)

public

bizexception

(string errorcode, string errormsg, throwable cause)

public string geterrorcode()

public

void

seterrorcode

(string errorcode)

public string geterrormsg()

public

void

seterrormsg

(string errormsg)

public string getmessage()

@override

public throwable fillinstacktrace()

}

順便這裡我們定義一下資料的傳輸格式。

public

class

resultbody

public

resultbody

(baseerrorinfointe***ce errorinfo)

public string getcode()

public

void

setcode

(string code)

public string getmessage()

public

void

setmessage

(string message)

public object getresult()

public

void

setresult

(object result)

/** * 成功

* * @return

*/public

static resultbody success()

/** * 成功

* @param data

* @return

*/public

static resultbody success

(object data)

/** * 失敗

*/public

static resultbody error

(baseerrorinfointe***ce errorinfo)

/** * 失敗

*/public

static resultbody error

(string code, string message)

/** * 失敗

*/public

static resultbody error

( string message)

@override

public string tostring()

}

@controlleradvice

public

class

globalexceptionhandler

",e.

geterrormsg()

);return resultbody.

error

(e.geterrorcode()

,e.geterrormsg()

);}/**

* 處理空指標的異常

/** * 處理其他異常

}

因為這裡我們只是用於做全域性異常處理的功能實現以及測試,所以這裡我們只需在新增乙個實體類和乙個控制層類即可。

實體類

public

class

user

implements

serializable

public

intgetid()

public

void

setid

(int id)

public string getname()

public

void

setname

(string name)

public

intgetage()

public

void

setage

(int age)

public string tostring()

}

controller 控制層

@restcontroller

(value =

"/user"

)public

class

userrestcontroller

return

true;}

("/update"

)public

boolean

update

(@requestbody user user)

("/delete"

)public

boolean

delete

(@requestbody user user)

("/findbyuser"

)public list

findbyuser

(user user)

}

然後呼叫就ok了

最後簡單梳理一下:首先,建立自定義異常類,繼承於runtimeexception。如果出異常,在@controlleradvice注釋下的異常處理類裡面捕獲,根據異常種類進行處理。

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

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

springboot全域性異常捕獲

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

SpringBoot全域性異常處理

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