SpringBoot使用Aspect切面攔截打印請求參數的示例代碼

AspectJ作為語言級別的AOP框架,功能相比於SpringAOP更加強大。SpringAOP旨在提供給用戶一個輕量級的AOP實現方案,它隻能應用在SpringIOC容器中管理的bean。而AspectJ旨在提供給用戶一個完整的AOP解決方案,它可以應用在所有的域對象中,下面給大傢介紹SpringBoot使用Aspect切面攔截打印請求參數的代碼。

引入依賴

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

也用到瞭fastjson打印參數 , 如果引瞭就不需要(也可以根據自己的來打印)

<!-- 添加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  //表示它是一個切面
public class LogAspect {

    /**
     *
     *  execution:改成自己要打印的控制器路徑
     * @param proceedingJoinPoint
     * @return
     * @throws Throwable
     */
    @Around("execution(* com.example.*.controller.*.*(..)) ")
    public Object handleControllerMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

        //原始的HTTP請求和響應的信息
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        Signature signature = proceedingJoinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature)signature;
        //獲取當前執行的方法
        Method targetMethod = methodSignature.getMethod();

        //獲取參數
        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();
    }
}

到此這篇關於SpringBoot使用Aspect切面攔截打印請求參數的文章就介紹到這瞭,更多相關SpringBoot打印請求參數內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: