Spring?AOP實(shí)現(xiàn)打印HTTP接口出入?yún)⑷罩?/h1>
更新時(shí)間:2022年09月26日 17:03:17 作者:IT小村
這篇文章主要為大家詳細(xì)介紹了Spring?AOP如何實(shí)現(xiàn)打印HTTP接口出入?yún)⑷罩竟δ埽闹械氖纠a講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
前言
最近在維護(hù)一個(gè)運(yùn)營端的系統(tǒng),和前端聯(lián)調(diào)的過程中,經(jīng)常需要排查一些交互上的問題,每次都得看前端代碼的傳參和后端代碼的出參,于是打算給HTTP接口加上出入?yún)⑷罩尽?/p>
但看著目前的HTTP接口有點(diǎn)多,那么有什么快捷的方式呢?答案就是實(shí)用Spring的AOP功能,簡單實(shí)用。
思路
定義個(gè)一個(gè)SpringAOP的配置類,里邊獲取請(qǐng)求的URL、請(qǐng)求的入?yún)ⅰ⑾鄳?yīng)的出參,通過日志打印出來。
SpringBoot的aop依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
示例

1.編寫一個(gè)HTTP接口
定義了一個(gè)Controller,里邊就一個(gè)方法,方法請(qǐng)求類型是get,出入?yún)⒍际呛唵蔚囊粋€(gè)字符串字段。
package com.example.springbootaoplog.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author hongcunlin
*/
@RestController
@RequestMapping("/index")
public class IndexController {
@GetMapping("/indexContent")
public String indexContent(String param) {
return "test";
}
}
2.編寫一個(gè)AOP日志配置
這算是本文的重點(diǎn)了,定義一個(gè)AOP的內(nèi)容,首先是切點(diǎn),再者是請(qǐng)求前日志打印,最后請(qǐng)求后日志打印
package com.example.springbootaoplog.config;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
/**
* aop日志打印配置
*
* @author hongcunlin
*/
@Slf4j
@Aspect
@Component
public class AopLogConfig {
/**
* 切點(diǎn)路徑:Controller層的所有方法
*/
@Pointcut("execution(public * com.example.springbootaoplog.controller.*.*(..))")
public void methodPath() {
}
/**
* 入?yún)?
*
* @param joinPoint 切點(diǎn)
*/
@Before(value = "methodPath()")
public void before(JoinPoint joinPoint) {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
String url = requestAttributes.getRequest().getRequestURL().toString();
log.info("請(qǐng)求 = {}, 入?yún)?= {}", url, JSON.toJSONString(joinPoint.getArgs()));
}
/**
* 出參
*
* @param res 返回
*/
@AfterReturning(returning = "res", pointcut = "methodPath()")
public void after(Object res) {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
String url = requestAttributes.getRequest().getRequestURL().toString();
log.info("請(qǐng)求 = {}, 入?yún)?= {}", url, JSON.toJSONString(res));
}
}
3.結(jié)果測(cè)試
我們通過瀏覽器的URL,針對(duì)我們編寫的http接口,發(fā)起一次get請(qǐng)求:

可以看到,日志里邊打印了我們預(yù)期的請(qǐng)求的URL和出入?yún)⒘耍?/p>

說明我們的程序是正確的了。
到此這篇關(guān)于Spring AOP實(shí)現(xiàn)打印HTTP接口出入?yún)⑷罩镜奈恼戮徒榻B到這了,更多相關(guān)Spring AOP打印日志內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
-
java實(shí)現(xiàn)KFC點(diǎn)餐系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)KFC點(diǎn)餐系統(tǒng),模擬肯德基快餐店的收銀系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下 2019-01-01
-
Spring使用AspectJ注解和XML配置實(shí)現(xiàn)AOP
這篇文章主要介紹了Spring使用AspectJ注解和XML配置實(shí)現(xiàn)AOP的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下 2016-10-10
-
淺談java 字符串,字符數(shù)組,list間的轉(zhuǎn)化
下面小編就為大家?guī)硪黄獪\談java 字符串,字符數(shù)組,list間的轉(zhuǎn)化。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧 2016-11-11
-
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(28)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你 2021-07-07
最新評(píng)論
前言
最近在維護(hù)一個(gè)運(yùn)營端的系統(tǒng),和前端聯(lián)調(diào)的過程中,經(jīng)常需要排查一些交互上的問題,每次都得看前端代碼的傳參和后端代碼的出參,于是打算給HTTP接口加上出入?yún)⑷罩尽?/p>
但看著目前的HTTP接口有點(diǎn)多,那么有什么快捷的方式呢?答案就是實(shí)用Spring的AOP功能,簡單實(shí)用。
思路
定義個(gè)一個(gè)SpringAOP的配置類,里邊獲取請(qǐng)求的URL、請(qǐng)求的入?yún)ⅰ⑾鄳?yīng)的出參,通過日志打印出來。
SpringBoot的aop依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
示例
1.編寫一個(gè)HTTP接口
定義了一個(gè)Controller,里邊就一個(gè)方法,方法請(qǐng)求類型是get,出入?yún)⒍际呛唵蔚囊粋€(gè)字符串字段。
package com.example.springbootaoplog.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author hongcunlin */ @RestController @RequestMapping("/index") public class IndexController { @GetMapping("/indexContent") public String indexContent(String param) { return "test"; } }
2.編寫一個(gè)AOP日志配置
這算是本文的重點(diǎn)了,定義一個(gè)AOP的內(nèi)容,首先是切點(diǎn),再者是請(qǐng)求前日志打印,最后請(qǐng)求后日志打印
package com.example.springbootaoplog.config; import com.alibaba.fastjson.JSON; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; /** * aop日志打印配置 * * @author hongcunlin */ @Slf4j @Aspect @Component public class AopLogConfig { /** * 切點(diǎn)路徑:Controller層的所有方法 */ @Pointcut("execution(public * com.example.springbootaoplog.controller.*.*(..))") public void methodPath() { } /** * 入?yún)? * * @param joinPoint 切點(diǎn) */ @Before(value = "methodPath()") public void before(JoinPoint joinPoint) { ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); String url = requestAttributes.getRequest().getRequestURL().toString(); log.info("請(qǐng)求 = {}, 入?yún)?= {}", url, JSON.toJSONString(joinPoint.getArgs())); } /** * 出參 * * @param res 返回 */ @AfterReturning(returning = "res", pointcut = "methodPath()") public void after(Object res) { ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); String url = requestAttributes.getRequest().getRequestURL().toString(); log.info("請(qǐng)求 = {}, 入?yún)?= {}", url, JSON.toJSONString(res)); } }
3.結(jié)果測(cè)試
我們通過瀏覽器的URL,針對(duì)我們編寫的http接口,發(fā)起一次get請(qǐng)求:
可以看到,日志里邊打印了我們預(yù)期的請(qǐng)求的URL和出入?yún)⒘耍?/p>
說明我們的程序是正確的了。
到此這篇關(guān)于Spring AOP實(shí)現(xiàn)打印HTTP接口出入?yún)⑷罩镜奈恼戮徒榻B到這了,更多相關(guān)Spring AOP打印日志內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實(shí)現(xiàn)KFC點(diǎn)餐系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)KFC點(diǎn)餐系統(tǒng),模擬肯德基快餐店的收銀系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01Spring使用AspectJ注解和XML配置實(shí)現(xiàn)AOP
這篇文章主要介紹了Spring使用AspectJ注解和XML配置實(shí)現(xiàn)AOP的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10淺談java 字符串,字符數(shù)組,list間的轉(zhuǎn)化
下面小編就為大家?guī)硪黄獪\談java 字符串,字符數(shù)組,list間的轉(zhuǎn)化。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(28)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07