使用SpringMVC處理全局異常不生效問題

問題描述

項目是老項目,使用springmvc跳轉頁面。需求是需要集中處理異常,做後續日誌、通知等處理。

@Component()
public class NotifyDingHandler extends BaseService implements HandlerExceptionResolver{
	@Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {

處理過程

但是在測試中發現沒有生效,會自己跳轉到500.jsp頁面,查詢網上方法,修改@Component(value=“handlerExceptionResolver”),但是我這並沒有生效。查資料原來是處理器順序問題,spring默認處理器將異常給處理導致。
在此感謝 butioy的文章 https://blog.csdn.net/butioy_org/article/details/78718405的幫助在這裏插入圖片描述
隨便查看一個默認處理器,查看父類都會實現Ordered接口
在這裏插入圖片描述
所以將自己的異常處理器進行如下改造,讓自定義處理器排在最前面進行處理

@Component()
public class NotifyDingHandler extends BaseService implements HandlerExceptionResolver, Ordered {
	private int order = Ordered.HIGHEST_PRECEDENCE;
	@Override
    public int getOrder() {
        return order;
    }
}

注意:此只針對老項目,其他兩種方法不會造成這種不生效問題,其他方法請自行查閱。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章