SpringBoot?AOP統(tǒng)一處理Web請求日志的示例代碼
SpringBoot AOP統(tǒng)一處理Web請求日志
引入依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
- 新建過濾器 WebLogAspect.java
- 使用 @Aspect 注解修飾:說明當(dāng)前類 是一個切面類。
- 使用 @Component注解修飾:標(biāo)記 切面類 為組件,這樣 IOC 容器 會為其實例化和管理(< bean>)
- 定義切面方法:webLog() 方法名隨意
- 使用環(huán)繞通知 @Around(“execution(public * com.daxiong.mall.Controller..(…))”) 注解修飾指定 切面范圍。
@Around("execution(public * com.daxiong.mall.controller.*.*(..))")
public Object webLog(ProceedingJoinPoint pjp) {}
記錄請求 URL:
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
log.info("URL : " + request.getRequestURL().toString());
請求類型:
log.info("HTTP_METHOD : " + request.getMethod());
IP 地址:
- 若是本地 localhost,則會返回 ipv6 0:0:0:0:0:0:0:1
log.info("IP : " + request.getRemoteAddr());
記錄請求的 類以及方法:
- 使用 環(huán)繞通知的 ProceedingJoinPoint 參數(shù)。
log.info("CLASS_METHOD : " + pjp.getSignature().getDeclaringTypeName() + "." + pjp.getSignature().getName());
記錄請求參數(shù):
log.info("ARGS : " + Arrays.toString(pjp.getArgs()));
執(zhí)行方法,處理請求
Object res = pjp.proceed(pjp.getArgs());
記錄響應(yīng)結(jié)果 使用 jackson 將帝鄉(xiāng)轉(zhuǎn)換為 json 格式。
log.info("RESPONSE : " + new ObjectMapper().writeValueAsString(res));全部:
/**
* 打印請求和響應(yīng)信息
*/
@Aspect
@Component
public class WebLogAspect {
private final Logger log = LoggerFactory.getLogger(WebLogAspect.class);
@Around("execution(public * com.daxiong.mall.controller.*.*(..))")
public Object webLog(ProceedingJoinPoint pjp) throws Throwable {
log.info("========================新的請求========================");
// 收到請求,記錄請求內(nèi)容
ServletRequestAttributes attributes =
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
log.info("URL : " + request.getRequestURL().toString());
log.info("HTTP_METHOD : " + request.getMethod());
// 若是 localhost,則會返回 0:0:0:0:0:0:0:1
log.info("IP : " + request.getRemoteAddr());
log.info("CLASS_METHOD : " + pjp.getSignature().getDeclaringTypeName() + "." + pjp.getSignature().getName());
log.info("ARGS : " + Arrays.toString(pjp.getArgs()));
// 執(zhí)行方法,處理請求
Object res = pjp.proceed(pjp.getArgs());
// 記錄響應(yīng)
log.info("RESPONSE : " + new ObjectMapper().writeValueAsString(res));
return res;
}
}
總結(jié)
到此這篇關(guān)于SpringBoot AOP統(tǒng)一處理Web請求日志的文章就介紹到這了,更多相關(guān)SpringBoot AOP統(tǒng)一處理Web請求日志內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot?Starter自定義全局加解密組件的詳細(xì)流程
SpringBoot?Starter作用將一組相關(guān)的依賴打包,簡化項目的配置和初始化過程,通過特定的Starter開發(fā)者可以快速的實現(xiàn)特定功能模塊的開發(fā)和擴(kuò)展,本文給大家介紹了SpringBoot?Starter自定義全局加解密組件的詳細(xì)流程,需要的朋友可以參考下2024-02-02
Java?swing實現(xiàn)應(yīng)用程序?qū)?shù)據(jù)庫的訪問問題
這篇文章主要介紹了Java?swing實現(xiàn)應(yīng)用程序?qū)?shù)據(jù)庫的訪問,本次實驗需要做一個GUI界面和一個連接查詢功能,在論壇上借鑒了其他大佬獲取網(wǎng)站內(nèi)容的部分代碼,然后自己做了一個及其簡陋的swing界面,算是把這個實驗完成了,需要的朋友可以參考下2022-09-09
IntelliJ?IDEA?代碼運行時中文出現(xiàn)亂碼問題及解決方法
在我們剛接觸到IDEA時,想美滋滋的敲一個“hello?world”來問候這個世界,但難免會遇到這種問題亂碼,這篇文章主要介紹了解決IntelliJ?IDEA?代碼運行時中文出現(xiàn)亂碼問題,需要的朋友可以參考下2023-09-09
基于Zookeeper實現(xiàn)服務(wù)注冊和服務(wù)發(fā)現(xiàn)功能
無論是采用SOA還是微服務(wù)架構(gòu),都需要使用服務(wù)注冊和服務(wù)發(fā)現(xiàn)組件,本文將基于 Zookeeper 實現(xiàn)服務(wù)注冊和服務(wù)發(fā)現(xiàn)功能,如果跟我一樣有同樣的困惑,希望可以通過本文了解其他組件如何使用 Zookeeper 作為注冊中心的工作原理2023-09-09
Java數(shù)據(jù)結(jié)構(gòu)之順序表的實現(xiàn)
線性表(linear?list)是n個具有相同特性的數(shù)據(jù)元素的有限序列。順序表是常見的線性表之一,本文將詳細(xì)講講順序表的原理與實現(xiàn),需要的可以參考一下2022-08-08

