AOP获取请求接口所有信息(入参类型、参数、执行时间、当前方法路径、响应参数、响应数据类型)

日志输出实体

package com.log;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.List;
import java.util.Map;

@Data
@AllArgsConstructor
public class LiveLog {
    // 请求路径
    private String requestPath;
    // 请求方法
    private String requestMethod;
    // 当前的时间戳
    private Long currentTimeMills;
    // 参数类型
    private Map<String, String> argsType;
    // 所有入参
    private List<Object> allArgs;
    // 响应数据
    private String responseData;
    // 当前执行耗时
    private String executeTimeMills;
    // 当前请求的方法路径:(类路径 + 方法名字)
    private String classMethodLocation;
    // 请求的IP
    private String remoteAddr;
    // 当前日期(yyyy-MM-dd HH:mm:ss)
    private String nowTime;
    // 响应数据类型
    private String responseType;
}

AOP切面类

package com.log.aspect;

import com.alibaba.fastjson.JSONObject;
import com.cmb.live.log.LiveLog;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
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.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Aspect
@Component
@Slf4j
public class LiveLogAspect {


    @Pointcut(value = "execution(public * com.controller.*.*(..))") // 切点
    private void controllerPointCut() {
    }


    @Around(value = "controllerPointCut()")
    private Object Around(ProceedingJoinPoint proceedingJoinPoint) {
        HttpServletRequest request = ((ServletRequestAttributes)  HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        String requestURI = request.getRequestURI();
        String requestMethod = request.getMethod();
        String remoteAddr = request.getRemoteAddr();

        Long startTime = System.currentTimeMillis();// 步入时间戳
        MethodSignature method = (MethodSignature) proceedingJoinPoint.getSignature();
        Class<?> currentClass = proceedingJoinPoint.getTarget().getClass();
        Object proceed = proceedingJoinPoint.proceed();
        List<Object> allArgs = Arrays.asList(proceedingJoinPoint.getArgs());
        List<Object> args = allArgs.stream().map(arg -> {
            if (!(arg instanceof HttpServletRequest) && !(arg instanceof HttpServletResponse)) {
                return arg;
            } else {
                return null;
            }
        }).filter(arg -> arg != null).collect(Collectors.toList());

        LiveLog liveLog = new LiveLog(requestURI, requestMethod, startTime,
                getMethodArgumentTypeName(method), args, proceed != null ? proceed.toString() : "null",
                (System.currentTimeMillis() - startTime) + "ms", currentClass.getName() + "." + method.getName(),
                remoteAddr, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")), method.getReturnType().getName());

        log.info(JSONObject.toJSONString(liveLog));
        return proceed;
    }

    @AfterThrowing(value = "controllerPointCut()")
    private void AfterThrowing() {
        System.out.println("异常通知");
    }

    private Map<String, String> getMethodArgumentTypeName(MethodSignature method) {
        Map<String, String> map = new HashMap<>();
        String[] argTypeNames = method.getParameterNames();
        Class[] parameterTypes = method.getParameterTypes();
        for (int i = 0; i < parameterTypes.length; i++) {
            map.put(parameterTypes[i].getName(), argTypeNames[i]);
        }
        return map;
    }
}

你可能感兴趣的:(Java,Spring,java,java-ee,spring)