SpringBoot使用Aspect切面攔截打印請(qǐng)求參數(shù)的示例代碼
AspectJ作為語言級(jí)別的AOP框架,功能相比于SpringAOP更加強(qiáng)大。SpringAOP旨在提供給用戶一個(gè)輕量級(jí)的AOP實(shí)現(xiàn)方案,它只能應(yīng)用在SpringIOC容器中管理的bean。而AspectJ旨在提供給用戶一個(gè)完整的AOP解決方案,它可以應(yīng)用在所有的域?qū)ο笾校旅娼o大家介紹SpringBoot使用Aspect切面攔截打印請(qǐng)求參數(shù)的代碼。
引入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId></dependency>
也用到了fastjson打印參數(shù) , 如果引了就不需要(也可以根據(jù)自己的來打印)
<!-- 添加fastjson 支持 --><dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.15</version></dependency>
LogAspect.java
import com.alibaba.fastjson.JSON;import lombok.extern.slf4j.Slf4j;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.Signature;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.reflect.MethodSignature;import org.springframework.stereotype.Component;import org.springframework.web.context.request.RequestContextHolder;import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest;import java.lang.reflect.Method;/** * @author zhipeih * @date 2021/07/14 */@Slf4j@Component@Aspect //表示它是一個(gè)切面public class LogAspect { /** * * execution:改成自己要打印的控制器路徑 * @param proceedingJoinPoint * @return * @throws Throwable */ @Around('execution(* com.example.*.controller.*.*(..)) ') public Object handleControllerMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {//原始的HTTP請(qǐng)求和響應(yīng)的信息ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletRequest request = attributes.getRequest();Signature signature = proceedingJoinPoint.getSignature();MethodSignature methodSignature = (MethodSignature)signature;//獲取當(dāng)前執(zhí)行的方法Method targetMethod = methodSignature.getMethod();//獲取參數(shù)Object[] objects = proceedingJoinPoint.getArgs();//獲取返回對(duì)象Object object = proceedingJoinPoint.proceed();StringBuilder sb = new StringBuilder(1000);sb.append('-------------------------------------------------------------n');sb.append('Controller: ').append(targetMethod.getDeclaringClass().getName()).append('n');sb.append('Method : ').append(targetMethod.getName()).append('n');sb.append('Params : ').append(JSON.toJSONString(objects)).append('n');sb.append('URI : ').append(request.getRequestURI()).append('n');sb.append('URL : ').append(request.getRequestURL()).append('n');sb.append('Return : ').append(object).append('n');sb.append('-------------------------------------------------------------n');System.out.println(sb);return proceedingJoinPoint.proceed(); }}
到此這篇關(guān)于SpringBoot使用Aspect切面攔截打印請(qǐng)求參數(shù)的文章就介紹到這了,更多相關(guān)SpringBoot打印請(qǐng)求參數(shù)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 以PHP代碼為實(shí)例詳解RabbitMQ消息隊(duì)列中間件的6種模式2. Python如何進(jìn)行時(shí)間處理3. python web框架的總結(jié)4. 詳解Python模塊化編程與裝飾器5. Python通過format函數(shù)格式化顯示值6. html小技巧之td,div標(biāo)簽里內(nèi)容不換行7. python裝飾器三種裝飾模式的簡單分析8. Python 如何將integer轉(zhuǎn)化為羅馬數(shù)(3999以內(nèi))9. Python實(shí)現(xiàn)迪杰斯特拉算法過程解析10. python使用ctypes庫調(diào)用DLL動(dòng)態(tài)鏈接庫
