• 【代码】Hutool工具增加拦截器打印请求和响应和配置


    show me the code:

    
    import cn.hutool.http.GlobalInterceptor;
    import cn.hutool.http.HttpGlobalConfig;
    import cn.hutool.http.HttpRequest;
    import cn.hutool.http.HttpResponse;
    import lombok.extern.slf4j.Slf4j;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    import java.util.Random;
    
    /**
     * @Description
     * @Date 2023/10/13
     * @Version 1.0
     **/
    @Slf4j
    @Component
    public class hutoolHttpUtilConfig {
        Logger logger = LoggerFactory.getLogger(HttpGlobalConfig.class);
        Double id ;
        @Autowired
        public void hutoolHttpUtilConfig() {
            // 设置hutool HttpUtil的request请求参数打印
    //        GlobalInterceptor.INSTANCE.addRequestInterceptor(httpObj -> log.info(String.valueOf(httpObj)));
    
            GlobalInterceptor.INSTANCE.addRequestInterceptor(httpObj -> this.wirteRequestLog(httpObj));
    
            // 设置hutool HttpUtil的response参数打印
    //        GlobalInterceptor.INSTANCE.addResponseInterceptor(httpObj -> log.info(String.valueOf(httpObj)));
            GlobalInterceptor.INSTANCE.addResponseInterceptor(httpObj -> this.writeResponseLog(httpObj));
    
            // 设置hutool HttpUtil的连接超时时间、读取响应超时时间
            HttpGlobalConfig.setTimeout(3000);
        }
    
        private Double wirteRequestLog(HttpRequest request){
            System.out.println(request);
            id = Math.random();
            System.out.println("request随机数" + id);
            return id;
        }
    
        private void writeResponseLog(HttpResponse response){
            System.out.println(response);
            System.out.println("response随机数" + id);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    下面额外记录使用aop来获取日志

    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.*;
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
    
    /**
     * @Description 记录调用第三方日志
     * @Date 2023/10/12
     * @Version 1.0
     **/
    @Aspect
    @Order(0)
    @Component
    public class AOPForAccessLog {
        @Pointcut("execution(* com.*.aiops.*.proxy.third.*.*.*(..))")
        public void thirdPartyMethod() {}
    
    
        @Before("thirdPartyMethod()")
        public void methodBefore(JoinPoint joinPoint) {
            Object[] args = joinPoint.getArgs();
            System.out.println("Method: " + joinPoint.getSignature().getName());
            for (Object arg : args) {
                System.out.println("Request Parameter: " + arg);
            }
        }
    
    //    @After(pointcut = "thirdPartyMethod() && executeMethod()", returning = "response")
        @AfterReturning(pointcut = "thirdPartyMethod() ", returning = "response")
        public void methodAfter(JoinPoint joinPoint, Object response) {
            System.out.println("Method: " + joinPoint.getSignature().getName());
            System.out.println("Response: " + response);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
  • 相关阅读:
    Node.js和浏览器在JavaScript运行环境方面存在一些区别和联系
    聚观早报 | 网传恒大员工“停工留职”;腾讯WiFi管家停止服务
    2024法定节假日|除夕不放假?企业这样做员工更满意
    (Research)肝肿瘤免疫微环境亚型和中性粒细胞异质性
    %md在printf语句和scanf语句中的区别
    如何在电脑上玩“胆小菇之梦”
    系列文章之一文纵览机器学习(6)——文本数据的转换处理 | 图像数据的转换处理 | 附:CNN激活层可视化(附源代码)
    2011年408真题复盘
    Flask Web——表单
    (动态)树分治 学习笔记
  • 原文地址:https://blog.csdn.net/xjw9602/article/details/133814789