Spring Boot中使用AOP統(tǒng)一處理web層異常的方法
在springboot錯誤默認是跳轉(zhuǎn)到 請求返回渲染路徑中的error/錯誤頁面中。
源碼分析: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中配置渲染頁面為
#配置freemaker spring.freemarker.template-loader-path=/WEB-INF/
如果不配置spring.freemarker.template-loader-path,springboot會在src/main/resources中的templates中的error文件下下找錯誤渲染的頁面。
那么當出現(xiàn)錯誤時,系統(tǒng)會跳轉(zhuǎn)到/WEB-INF/error/錯誤頁面中。

使用AOP進行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);
/**
* 匿名切點的方式
*
* @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("拋出運行時異常{}", ex.getMessage());
modelAndView.addObject("exception", ex.getMessage());
// 跳轉(zhuǎn)到錯誤頁面
modelAndView.addObject("url", request.getRequestURL());
return modelAndView;
}
modelAndView.addObject("exception","未知異常");
return modelAndView;
}
}
總結
以上所述是小編給大家介紹的Spring Boot中使用AOP統(tǒng)一處理web層異常,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
shiro并發(fā)人數(shù)登錄控制的實現(xiàn)代碼
在做項目中遇到這樣的需求要求每個賬戶同時只能有一個人登錄或幾個人同時登錄,如果是同時登錄的多人,要么不讓后者登錄,要么踢出前者登錄,怎么實現(xiàn)這樣的功能呢?下面小編給大家?guī)砹藄hiro并發(fā)人數(shù)登錄控制的實現(xiàn)代碼,一起看看吧2017-09-09
解決問題:Failed to execute goal org.apache.m
這篇文章主要給大家介紹了關于解決問題:Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources的相關資料,文中將解決的辦法介紹的非常詳細,需要的朋友可以參考下2023-03-03
Java實現(xiàn)獲取內(nèi)網(wǎng)的所有IP地址
這篇文章主要介紹了如何利用Java語言實現(xiàn)獲取內(nèi)網(wǎng)的所有IP地址,文中的示例代碼講解詳細,對我們學習有一定的參考價值,快跟隨小編一起學習一下吧2022-06-06

