获取request body入参并打印日志, aop实现

在无法使用spring提供的增强方法时,可以自定义实现打印request body入参

@Aspect
@Component
@Slf4j
public class ControllerAspect {

    private final HttpServletRequest request;

    public ControllerAspect(HttpServletRequest request) {
        this.request = request;
    }

    @Pointcut("execution(* com.hst.project.sceneweb.web.controller.*Controller*.*(..))")
    public void controllerAspect() {

    }

    @Around(value = "controllerAspect()", argNames = "pjp")
    private AjaxResult doAround(ProceedingJoinPoint pjp) throws Exception {
        try {
            String method = request.getMethod();
            Object[] args = pjp.getArgs();
            String params = "";
            //获取请求参数集合并进行遍历拼接
            if (args.length > 0) {
                if (!"GET".equals(method)) {
                    log.info("请求query入参===" + request.getQueryString());
                    Object object = args[0];
                    Map map = getKeyAndValue(object);
                    params = JSON.toJSONString(map);
                    log.info("请求body 入参===" + params);
                } else {
                    log.info("请求query入参===" + request.getQueryString());
                }
            }
            // result的值就是被拦截方法的返回值
            Object result = pjp.proceed();
            AjaxResult ajaxResult = (AjaxResult) result;
            if (BeanUtil.checkIsNotEmpty(ajaxResult)) {
                Params ajaxResultParams = new Params();
                if (BeanUtil.checkIsNotEmpty(request.getQueryString())) {
                    ajaxResultParams.setQueryParams(URLDecoder.decode(request.getQueryString(), "UTF-8"));
                }
                ajaxResultParams.setBodyParams(JSON.parseObject(params));
                ajaxResult.setParams(ajaxResultParams);
                log.info("请求出参     ===" + ajaxResult.toString());
            }
            return (AjaxResult) result;
        } catch (Throwable throwable) {
            if (throwable instanceof ParamException) {
                throw new ParamException(throwable.getMessage());
            } else if (throwable instanceof HttpException) {
                throw new HttpException(((HttpException) throwable).getCode(), throwable.getMessage());
            } else if (throwable instanceof ServiceException) {
                throw new ServiceException(throwable.getMessage());
            } else if (throwable instanceof InvokeException) {
                throw new InvokeException(throwable.getMessage());
            } else {
                throw new Exception(throwable);
            }
        }
    }


    private static Map<String, Object> getKeyAndValue(Object obj) {
        Map<String, Object> map = Maps.newHashMapWithExpectedSize(10);
        // 得到类对象
        Class userCla = obj.getClass();
        /* 得到类中的所有属性集合 */
        Field[] fs = userCla.getDeclaredFields();
        for (Field f : fs) {
            // 设置些属性是可以访问的
            f.setAccessible(true);
            Object val;
            try {
                // 得到此属性的值
                val = f.get(obj);
                // 设置键值
                map.put(f.getName(), val);
            } catch (IllegalArgumentException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return map;
    }

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在AOP获取`@RequestBody`的信息,可以使用`JoinPoint`参数和`MethodSignature`对象来获取方法的参数信息。然后,通过遍历参数注解数组,找到带有`@RequestBody`注解的参数,并获取相应的信息。 以下是一个示例代码: ```java @Aspect @Component public class RequestBodyAspect { @Before("execution(* com.example.controller.*.*(..))") public void logRequestBody(JoinPoint joinPoint) throws NoSuchMethodException { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); String[] parameterNames = signature.getParameterNames(); Annotation[][] parameterAnnotations = signature.getMethod().getParameterAnnotations(); for (int i = 0; i < parameterAnnotations.length; i++) { for (Annotation annotation : parameterAnnotations[i]) { if (annotation instanceof RequestBody) { System.out.println("RequestBody parameter name: " + parameterNames[i]); // 获取参数类型 Class<?> parameterType = signature.getMethod().getParameterTypes()[i]; System.out.println("RequestBody parameter type: " + parameterType); // 其他操作... } } } } } ``` 在上述示例中,切面类`RequestBodyAspect`使用`@Before`注解标记了一个方法`logRequestBody`,该方法会在目标方法执行前执行。通过遍历参数注解数组,找到带有`@RequestBody`注解的参数,并获取参数的名称和类型。 请注意,由于`@RequestBody`注解通常用于接收请求体中的JSON或XML等数据,因此参数类型可能是自定义的POJO类或其他类型。您可以根据实际需要进行进一步操作,例如将请求体转换为对象或执行其他逻辑处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值