springboot面向切面http请求信息及返回信息日志输出(注解)

public class VersionManageAspect {

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


    @Pointcut("execution(public * com.sinosoft.mobile.platform.controller.versionManage..*(..)) && !execution(public * com.sinosoft.mobile.platform.controller.versionManage.PackageManageController.downloadUpdatePackage(..))")
    public void dataLog() {
    }

    @Before("dataLog()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        // 接收到请求,记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();


        // 记录下请求内容
        logger.info("请求的URL : " + request.getRequestURL().toString());
        //logger.info("HTTP_METHOD : " + request.getMethod());
        // logger.info("IP : " + request.getRemoteAddr());
        logger.info("请求方法 : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        //使用的是spring内自带的json解析工具jackson
        logger.info("请求参数 : " + new ObjectMapper().writeValueAsString(request.getParameterMap() == null || request.getParameterMap().isEmpty() ? joinPoint.getArgs() : request.getParameterMap()));

    }

    @AfterReturning(returning = "ret", pointcut = "dataLog()")
    public void doAfterReturning(Object ret) throws Throwable {
        // 处理完请求,返回内容
        logger.info("返回的内容 : " + ret);
    }
}
 

你可能感兴趣的:(springboot)