Spring Boot中使用AOP統(tǒng)一處理web層異常的方法
在springboot錯(cuò)誤默認(rèn)是跳轉(zhuǎn)到 請(qǐng)求返回渲染路徑中的error/錯(cuò)誤頁(yè)面中。
源碼分析:DefaultErrorViewResolver.java
private ModelAndView resolve(String viewName, Map<String, Object> model) {
String errorViewName = "error/" + viewName;
TemplateAvailabilityProvider provider = this.templateAvailabilityProviders
.getProvider(errorViewName, this.applicationContext);
if (provider != null) {
return new ModelAndView(errorViewName, model);
}
return resolveResource(errorViewName, model);
}
比如在application.properites中配置渲染頁(yè)面為
#配置freemaker spring.freemarker.template-loader-path=/WEB-INF/
如果不配置spring.freemarker.template-loader-path,springboot會(huì)在src/main/resources中的templates中的error文件下下找錯(cuò)誤渲染的頁(yè)面。
那么當(dāng)出現(xiàn)錯(cuò)誤時(shí),系統(tǒng)會(huì)跳轉(zhuǎn)到/WEB-INF/error/錯(cuò)誤頁(yè)面中。

使用AOP進(jìn)行web層異常處理
package com.niugang.aop;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;
/**
* controller層統(tǒng)一異常處理
*
* @author niugang
*
*/
@Aspect
@Component
public class ExceptionControllerAscept {
private Logger logger = LoggerFactory.getLogger(ExceptionControllerAscept.class);
/**
* 匿名切點(diǎn)的方式
*
* @param ex
* @throws ServletException
* @throws IOException
*/
@AfterThrowing(value = "execution(public * com.niugang.controller..*.*(..))", throwing = "ex")
public ModelAndView aroundAdvice(Exception ex) throws ServletException, IOException {
ModelAndView modelAndView = new ModelAndView();
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes r = (ServletRequestAttributes) requestAttributes;
HttpServletRequest request = r.getRequest();
modelAndView.setViewName("500");
// 第一如果是 RuntimeException
if (ex instanceof RuntimeException) {
logger.error("拋出運(yùn)行時(shí)異常{}", ex.getMessage());
modelAndView.addObject("exception", ex.getMessage());
// 跳轉(zhuǎn)到錯(cuò)誤頁(yè)面
modelAndView.addObject("url", request.getRequestURL());
return modelAndView;
}
modelAndView.addObject("exception","未知異常");
return modelAndView;
}
}
總結(jié)
以上所述是小編給大家介紹的Spring Boot中使用AOP統(tǒng)一處理web層異常,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
shiro并發(fā)人數(shù)登錄控制的實(shí)現(xiàn)代碼
在做項(xiàng)目中遇到這樣的需求要求每個(gè)賬戶(hù)同時(shí)只能有一個(gè)人登錄或幾個(gè)人同時(shí)登錄,如果是同時(shí)登錄的多人,要么不讓后者登錄,要么踢出前者登錄,怎么實(shí)現(xiàn)這樣的功能呢?下面小編給大家?guī)?lái)了shiro并發(fā)人數(shù)登錄控制的實(shí)現(xiàn)代碼,一起看看吧2017-09-09
Maven高級(jí)的聚合和繼承的實(shí)現(xiàn)
在軟件開(kāi)發(fā)中,隨著項(xiàng)目規(guī)模的擴(kuò)大,單個(gè)模塊的開(kāi)發(fā)方式逐漸轉(zhuǎn)變?yōu)槎嗄K開(kāi)發(fā),這種方式帶來(lái)了項(xiàng)目管理上的挑戰(zhàn),其中最常見(jiàn)的問(wèn)題是模塊間的依賴(lài)管理和版本控制問(wèn)題,本文就來(lái)介紹一下2024-10-10
SpringBoot3各種配置的優(yōu)先級(jí)對(duì)比小結(jié)
SpringBoot3提供了多種配置來(lái)源以滿(mǎn)足不同場(chǎng)景下的需求,本文詳細(xì)介紹了SpringBoot3中的配置優(yōu)先級(jí)對(duì)比小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2024-12-12
Java 8 對(duì) HashSet 元素進(jìn)行排序的操作方法
Java 中HashSet是一個(gè)不保證元素順序的集合類(lèi),其內(nèi)部是基于 HashMap 實(shí)現(xiàn)的,HashSet不支持排序,我們?cè)谛枰獙?duì)HashSet 排序時(shí),必須將其轉(zhuǎn)換為支持排序的集合或數(shù)據(jù)結(jié)構(gòu),如 List,本文將詳細(xì)介紹在 Java 8 中如何對(duì) HashSet 中的元素進(jìn)行排序,感興趣的朋友一起看看吧2024-11-11
Mybatis一對(duì)多查詢(xún)列表屬性處理示例詳解
使用MyBatis進(jìn)行多表聯(lián)查的關(guān)鍵是構(gòu)建數(shù)據(jù)庫(kù)中表的字段和java中對(duì)象的屬性的映射關(guān)系,下面這篇文章主要給大家介紹了關(guān)于Mybatis一對(duì)多查詢(xún)列表屬性處理的相關(guān)資料,需要的朋友可以參考下2023-05-05
解決問(wèn)題:Failed to execute goal org.apache.m
這篇文章主要給大家介紹了關(guān)于解決問(wèn)題:Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources的相關(guān)資料,文中將解決的辦法介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03
Java實(shí)現(xiàn)獲取內(nèi)網(wǎng)的所有IP地址
這篇文章主要介紹了如何利用Java語(yǔ)言實(shí)現(xiàn)獲取內(nèi)網(wǎng)的所有IP地址,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)有一定的參考價(jià)值,快跟隨小編一起學(xué)習(xí)一下吧2022-06-06
MyBatis圖文并茂講解注解開(kāi)發(fā)多對(duì)多查詢(xún)
這篇文章主要介紹了SpringBoot中Mybatis注解多對(duì)多查詢(xún)的實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07

