SpringBoot 拦截请求打印日志

SpringBoot 拦截请求打印日志

java 复制代码
import lombok.extern.slf4j.Slf4j;

import cn.hutool.json.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

@Slf4j
@Aspect
@Component
public class BusDealConf {

    @Around("@within(org.springframework.web.bind.annotation.RestController)" +
            "||@within(org.springframework.stereotype.Controller)")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        log.info("----------------------------------------开始----------------------------------------");
        if (null != attributes) {
            HttpServletRequest request = attributes.getRequest();
            log.info("请求路径: {}", request.getRequestURI());
            log.info("请求方式: {}", request.getMethod());
            log.info("请求方法: {}", point.getSignature());
            log.info("请求参数: {}", JSONUtil.toJsonPrettyStr(getPara(point.getArgs())));
        }

        long t1 = System.currentTimeMillis();
        Object result = point.proceed(point.getArgs());
        long t2 = System.currentTimeMillis();
        //log.info("响应参数: {}", JSONUtil.toJsonPrettyStr(result));
        log.info("执行耗时: {}ms", t2 - t1);
        log.info("----------------------------------------结束----------------------------------------");
        return result;
    }

    private List<Object> getPara(Object[] objects) {
        return Arrays.stream(objects)
                .filter(obj -> !(obj instanceof MultipartFile)
                && !(obj instanceof HttpServletResponse)
                && !(obj instanceof HttpServletRequest)).collect(Collectors.toList());
    }

}
相关推荐
猫头虎6 小时前
如何排查并解决项目启动时报错Error encountered while processing: java.io.IOException: closed 的问题
java·开发语言·jvm·spring boot·python·开源·maven
MZ_ZXD0018 小时前
springboot旅游信息管理系统-计算机毕业设计源码21675
java·c++·vue.js·spring boot·python·django·php
invicinble8 小时前
springboot的核心实现机制原理
java·spring boot·后端
space62123279 小时前
在SpringBoot项目中集成MongoDB
spring boot·后端·mongodb
金牌归来发现妻女流落街头10 小时前
【从SpringBoot到SpringCloud】
java·spring boot·spring cloud
皮卡丘不断更11 小时前
手搓本地 RAG:我用 Python 和 Spring Boot 给 AI 装上了“实时代码监控”
人工智能·spring boot·python·ai编程
lucky670711 小时前
Spring Boot集成Kafka:最佳实践与详细指南
spring boot·kafka·linq
Coder_Boy_11 小时前
基于Spring AI的分布式在线考试系统-事件处理架构实现方案
人工智能·spring boot·分布式·spring
毕设源码-钟学长13 小时前
【开题答辩全过程】以 基于Springboot的扶贫众筹平台为例,包含答辩的问题和答案
java·spring boot·后端