首页 > 代码库 > spring aop 实现请求报文打印

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 实现请求报文打印