码迷,mamicode.com
首页 > 编程语言 > 详细

spring aop 实现请求报文打印

时间:2017-06-17 18:28:23      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:with   string   ring   ase   aspect   null   util   auth   ref   

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

标签:with   string   ring   ase   aspect   null   util   auth   ref   

原文地址:http://www.cnblogs.com/anpieBlog/p/7040783.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!