使用 @exceptionhandler 註解
實現 handlerexceptionresolver 介面
使用 @controlleradvice 註解
當以上三個方式,一起出現時,只執行執行範圍最小的方式,後面的方式不再執行處理;執行範圍:@exceptionhandler < @[email protected] < handlerexceptionresolver
作用域:區域性,針對乙個controller中的指定異常類;
@controller
@responsebody
public class cors
// @exceptionhandler 引數為 要處理器的 異常類(或子類)
@exceptionhandler(arithmeticexception.class)
public string exinfo(arithmeticexception e)
}
作用域:全域性,針對全部controller,無需指定某個異常類;handlerexceptionresolver 介面 中 只有 resolveexception 乙個方法,實現即可;
@component
public class handlerex implements handlerexceptionresolver
}
作用域:全域性,針對全部controller中的指定異常類;該註解需要 和@exceptionhandler配合,來完成全域性異常的處理;
@controller
@responsebody
public class cors
}@controlleradvice
@responsestatus(httpstatus.bad_request) // 響應 狀態碼400
// @exceptionhandler 引數為 要處理器的 異常類(或子類)
// 註解引數不宣告指定異常類,則預設為方法列表中的異常引數類
@exceptionhandler(arithmeticexception.class)
public void showinfo(exception e)
}
此時,依然堅持寫在同乙個controler裡的話,未嘗不可。不改變的是:作用域仍是全域性的;例子如下SpringMVC統一異常處理
errors error prop errors errprop property name statuscodes 500prop 404prop property name warnlogcategory value warn property name defaulterrorview val...
springMVC統一異常處理
restcontrolleradvice basepackages controller包位址 slf4j public class gulimallexceptioncontrolleradvice 異常型別 e.getmessage e.getclass bindingresult bindin...
springMVC統一異常處理
實現spring的異常處理介面handlerexceptionresolver 自定義自己的異常處理器 使用 exceptionhandler註解實現異常處理 springmvc servlet.xml新增 error parameter public class myexceptionresolv...