spring aop 实现请求报文打印

Posted 根目录97

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring aop 实现请求报文打印相关的知识,希望对你有一定的参考价值。

利用spring aop 实现前端请求后台的参数打印。

/*
*===============================================================================================
*   author:          time:                             version:           desc:
*   anear           2017/2/26  13:25                      1.0
*===============================================================================================
*/
package com.qianmo.foru.process;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Arrays;


/**
 *
 */
@Component("aspect")
@Aspect
public class ExceptionAspect {
    private Logger logger = LoggerFactory.getLogger(ExceptionAspect.class);

    public ExceptionAspect() {
        logger.info("------------------------>请求报文 拦截[已加载]<------------------------");
    }


    @Around(value = "within(com.qianmo.foru.service.impl.PayServiceImpl)")
    public Object payAspect(ProceedingJoinPoint target) throws Throwable {

        Object answer = null;

//        MethodSignature signature = (MethodSignature) target.getSignature();
//        Method method = signature.getMethod();
//        logger.debug("方法名:" + method.getName());
//        if ("executeNotification".equals(method.getName())) {
//
//            Object[] objects = target.getArgs();
//            HttpServletRequest request = null;
//            for (int i = 0; i < objects.length; i++) {
//                if (objects[i] instanceof HttpServletRequest) {
//                    request = (HttpServletRequest) objects[i];
//                    if (Constants.HTTP_METHOD.post.equalsIgnoreCase(request.getMethod())) {
//                        logger.info("请求报文: {}", HttpUtil.extractPostRequestBody(request));
//                    }
//                } else {
//                    break;
//                }
//            }
//
//
//        }
        logger.info("请求报文: {}", Arrays.toString(target.getArgs()));
        answer = target.proceed();
        return answer;
    }

    @Around(value = "within(com.qianmo.foru.service.impl.RefundServiceImpl)")
    public Object refundAspect(ProceedingJoinPoint target) throws Throwable {
        Object answer = null;
        logger.info("请求报文: {}", Arrays.toString(target.getArgs()));
        answer = target.proceed();
        return answer;
    }

    @Around(value = "within(com.qianmo.foru.service.impl.OrderInfoServiceImpl)")
    public Object orderAspect(ProceedingJoinPoint target) throws Throwable {
        Object answer = null;
        logger.info("请求报文: {}", Arrays.toString(target.getArgs()));
        answer = target.proceed();
        return answer;
    }


}

不想多说,指向serviceImpl,然后在serviceImpl里的方法前加 

    @Transactional

加上这个注解,就可以实现了,效果如下:

请求报文: [{"orderFinishStat":"00","logger":{"eventLogger":false,"name":"com.qianmo.foru.bean.request.QueryOrderByAllRequest"}}]
16:59:14.847 [http-bio-8088-exec-14] INFO  com.qianmo.foru.service.impl.OrderInfoServiceImpl - ---------进入查询订单接口----------

 

以上是关于spring aop 实现请求报文打印的主要内容,如果未能解决你的问题,请参考以下文章

Spring aop 实例(转)

用spring aop实现动态代理的例子

spring boot aop打印http请求回复日志包含请求体

Spring的AOP快速实现通用日志打印

JAVA之AOP

解析Spring AOP执行流程