SpringBoot使用Aspect切面攔截打印請求參數(shù)的示例代碼
AspectJ作為語言級別的AOP框架,功能相比于SpringAOP更加強(qiáng)大。SpringAOP旨在提供給用戶一個(gè)輕量級的AOP實(shí)現(xiàn)方案,它只能應(yīng)用在SpringIOC容器中管理的bean。而AspectJ旨在提供給用戶一個(gè)完整的AOP解決方案,它可以應(yīng)用在所有的域?qū)ο笾校旅娼o大家介紹SpringBoot使用Aspect切面攔截打印請求參數(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請求和響應(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();//獲取返回對象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切面攔截打印請求參數(shù)的文章就介紹到這了,更多相關(guān)SpringBoot打印請求參數(shù)內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP中if語句、select 、while循環(huán)的使用方法2. 利用FastReport傳遞圖片參數(shù)在報(bào)表上展示簽名信息的實(shí)現(xiàn)方法3. CSS 使用Sprites技術(shù)實(shí)現(xiàn)圓角效果4. 詳解瀏覽器的緩存機(jī)制5. phpstudy apache開啟ssi使用詳解6. chat.asp聊天程序的編寫方法7. ASP常用日期格式化函數(shù) FormatDate()8. 怎樣才能用js生成xmldom對象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?9. HTML中的XML數(shù)據(jù)島記錄編輯與添加10. 推薦一個(gè)好看Table表格的css樣式代碼詳解
