zoukankan      html  css  js  c++  java
  • AOP打印请求日志,打印返回值

    @Aspect
    // 申明是个spring管理的bean
    @Component
    @Slf4j
    public class LogAspectServiceApi {
        private JSONObject jsonObject = new JSONObject();
    
        // 申明一个切点 里面是 execution表达式
        @Pointcut("execution(public * com.itmayiedu.api.service.*.*(..))")
        private void controllerAspect() {
        }
    
        // 请求method前打印内容
        @Before(value = "controllerAspect()")
        public void methodBefore(JoinPoint joinPoint) {
            ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
                    .getRequestAttributes();
            HttpServletRequest request = requestAttributes.getRequest();
            log.info("===============请求内容===============");
            try {
                // 打印请求内容
                log.info("请求地址:" + request.getRequestURL().toString());
                log.info("请求方式:" + request.getMethod());
                log.info("请求类方法:" + joinPoint.getSignature());
                log.info("请求类方法参数:" + JSONObject.toJSONString(joinPoint.getArgs(),true);
          } catch (Exception e) { log.error("###LogAspectServiceApi.class methodBefore() ### ERROR:", e); } log.info("===============请求内容==============="); } 

    // 在方法执行完结后打印返回内容
    @AfterReturning(returning = "o", pointcut = "controllerAspect()")
    public void methodAfterReturing(Object o) {
    log.info(
    "--------------返回内容----------------");
    try {
    log.info(
    "Response内容:" + jsonObject.toJSONString(o));
    }
    catch (Exception e) {
    log.error(
    "###LogAspectServiceApi.class methodAfterReturing() ### ERROR:", e);
    } log.info(
    "--------------返回内容----------------");
    }
    }
  • 相关阅读:
    svn使用
    navicat 15 安装破解
    thinkpad交换Fn和Ctrl
    emqx_mqtt安装+mqtt管理工具
    Adobe XD使用
    FolderPainter:windows系统为文件夹设置不同颜色
    rest client 代替postman
    使用bfg快速清理git历史大文件
    Adobe Acrobat XI Pro v11.0.10中文版
    Excel 2016打开文档时提示“操作系统当前的配置不能运行此应用程序”
  • 原文地址:https://www.cnblogs.com/brxHqs/p/10335582.html
Copyright © 2011-2022 走看看