Spring Boot中使用AOP統(tǒng)一處理web層異常的方法
在springboot錯(cuò)誤默認(rèn)是跳轉(zhuǎn)到 請(qǐng)求返回渲染路徑中的error/錯(cuò)誤頁面中。
源碼分析: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
會(huì)在src/main/resources
中的templates中的error文件下下找錯(cuò)誤渲染的頁面。
那么當(dāng)出現(xiàn)錯(cuò)誤時(shí),系統(tǒng)會(huì)跳轉(zhuǎn)到/WEB-INF/error/錯(cuò)誤頁面中。
使用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ò)誤頁面 modelAndView.addObject("url", request.getRequestURL()); return modelAndView; } modelAndView.addObject("exception","未知異常"); return modelAndView; } }
總結(jié)
以上所述是小編給大家介紹的Spring Boot中使用AOP統(tǒng)一處理web層異常,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Java工程中使用Mybatis (工程結(jié)合Mybatis,數(shù)據(jù)結(jié)合Swing使用))
這篇文章主要介紹了Java工程中使用Mybatis (工程結(jié)合Mybatis,數(shù)據(jù)可以結(jié)合Swing使用),需要的朋友可以參考下2017-04-04springboot整合mybatis-plus逆向工程的實(shí)現(xiàn)
這篇文章主要介紹了springboot整合mybatis-plus逆向工程的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Java四舍五入時(shí)保留指定小數(shù)位數(shù)的五種方式
這篇文章主要介紹了Java四舍五入時(shí)保留指定小數(shù)位數(shù)的五種方式,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2020-09-09SpringBoot+VUE實(shí)現(xiàn)前后端分離的實(shí)戰(zhàn)記錄
這篇文章主要介紹了SpringBoot+VUE實(shí)現(xiàn)前后端分離的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04如何使用MybatisPlus快速進(jìn)行增刪改查詳解
增刪改查在日常開發(fā)中是再正常不多的一個(gè)需求了,下面這篇文章主要給大家介紹了關(guān)于如何使用MybatisPlus快速進(jìn)行增刪改查的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08springboot 實(shí)現(xiàn)mqtt物聯(lián)網(wǎng)的示例代碼
這篇文章主要介紹了springboot 實(shí)現(xiàn)mqtt物聯(lián)網(wǎng),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03使用lombok注解導(dǎo)致mybatis-plus TypeHandler失效的解決
這篇文章主要介紹了使用lombok注解導(dǎo)致mybatis-plus TypeHandler失效的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07elasticsearch中term與match的區(qū)別講解
今天小編就為大家分享一篇關(guān)于elasticsearch中term與match的區(qū)別講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-02-02