• 【代码】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
  • 相关阅读:
    视频怎么做成二维码?在线教学视频码的制作技巧
    JuiceFS 在多云存储架构中的应用 | 深势科技分享
    8.Ribbon负载均衡服务调用
    对比多家互联网医院系统技术代码:数字医疗服务的背后
    Vue异步更新机制、$nextTick实现同步更新
    [AUTOSAR][诊断管理][ECU][$34] 下载请求
    谈谈如何进阶Java高级工程师
    Android12之DRM架构(一)
    System.Data.SqlClient.SqlError: 因为数据库正在使用,所以无法获得对数据库的独占访问权。
    apollo中配置map,list
  • 原文地址:https://blog.csdn.net/xjw9602/article/details/133814789