Spring Controller请求参数Log

package com.example.aop;

import com.alibaba.fastjson.JSON;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
/**
 *@author: youthcool
 *@date: 2019/9/6
 *@description: 请求参数Log
 */
@Component
@Aspect
public class ControllerLog {

    private final static Logger logger= LoggerFactory.getLogger(ControllerLog.class);

    @Pointcut("execution(* com.example.controller..*(..))")
    public void controller() { }

    @Before("controller()")
    public void doBefore(JoinPoint joinPoint) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String url=request.getRequestURI();
        Object[] args=joinPoint.getArgs();
        if(logger.isDebugEnabled()){
             logger.debug("method: {},url: {},args:{}",request.getMethod(),url, JSON.toJSONString(args));
        }
    }
}

 

你可能感兴趣的:(java)