aop springboot 传入参数_SpringBoot使用AOP获取请求参数

package com.*.aop;importcom.alibaba.fastjson.JSON;importcom.alibaba.fastjson.JSONArray;importcom.alibaba.fastjson.JSONObject;importlombok.extern.slf4j.Slf4j;importorg.aspectj.lang.ProceedingJoinPoint;importorg.aspectj.lang.Signature;importorg.aspectj.lang.annotation.Around;importorg.aspectj.lang.annotation.Aspect;importorg.aspectj.lang.annotation.Pointcut;importorg.springframework.stereotype.Component;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.util.ArrayList;importjava.util.List;/*** @Description //请求参数aop

**/@Component

@Aspect

@Slf4jpublic classRequestParameterAop {/*** @Description: 定义需要拦截的切面

* @Return: void

* @Author: yangli

* @Date: 2019/9/06-10:17

**/@Pointcut("execution(* com.*.controller.*Controller.*(..))")public voidmethodArgs() {

}

@Around("methodArgs()")public Object invoke(ProceedingJoinPoint joinPoint) throwsThrowable {

Object result= null;//获取请求参数进行打印

Signature signature =joinPoint.getSignature();

JSONObject signatureJson=JSON.parseObject(JSON.toJSONString(signature));//方法名

String methodName = signatureJson.getString("name");//类名

String serviceName = signatureJson.getString("declaringType");//参数名数组

JSONArray parameterNames = signatureJson.getJSONArray("parameterNames");//构造参数组集合

List argList = new ArrayList<>();for(Object arg : joinPoint.getArgs()) {//request/response无法使用toJSON

if (arg instanceofHttpServletRequest) {

argList.add("request");

}else if (arg instanceofHttpServletResponse) {

argList.add("response");

}else{

argList.add(JSON.toJSON(arg));

}

}try{

log.error("{} -> 方法({}) -> 参数:{} - {}", serviceName, methodName, JSON.toJSON(parameterNames), JSON.toJSON(argList));

}catch(Exception e) {

log.error("参数获取失败: {}", e.getMessage());

}

result=joinPoint.proceed();returnresult;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值