controller输入输出参数打印

package com.XX.project.backend.printParam;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 *  @description:
 *  @author: 
 *  @create: 2023/9/26 15:31
 * 说明:在方法上添加@PrintParams注解即可
 * 方式1(默认同时打印入参和返回结果):@PrintParams
 * 方式2(可选打印入参或返回结果),如打印入参但不打印返回结果:@PrintParams(requestParam = true,responseParam = false)
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface PrintParams {
    /**
     * 是否打印请求参数
     * @return
     */
    boolean requestParam() default true;

    /**
     * 是否打印返回结果
     * @return
     */
    boolean responseParam() default true;
}
package com.XX.project.backend.printParam;

import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @description:
 * @author: 
 * @create: 2023/9/26 15:31
 * restful接口请求参数打印切面
 * 说明:在方法上添加@PrintParams注解即可
 * 方式1(默认同时打印入参和返回结果):@PrintParams
 * 方式2(可选打印入参或返回结果),如打印入参但不打印返回结果:@PrintParams(requestParam = true,responseParam = false)
 */
@Aspect
@Component
public class PrintParamsAspect {

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

    @Around(value = "@annotation(PrintParams)")
    public Object printParams(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        String methodName = method.getName();
        Object[] args = joinPoint.getArgs();
        String declaringTypeName = signature.getDeclaringTypeName();
        String methodFullPath = declaringTypeName+"."+method.getName();
        String[] paramNames = signature.getParameterNames();
        PrintParams printParams = method.getAnnotation(PrintParams.class);
        try {
            if(printParams.requestParam()){
                logger.info("调用接口{},请求入参:参数名{},参数值{}", methodFullPath, Arrays.toString(paramNames), Arrays.toString(args));
            }
        }catch (Exception e){
            logger.error("打印请求参数失败");
        }
        Object result = joinPoint.proceed();
        try {
            if(printParams.responseParam()){
                logger.info("调用接口{},返回结果:{}", methodFullPath, JSONObject.toJSONString(result));
            }
        }catch (Exception e){
            logger.error("打印返回结果参数失败");
        }
        return result;
    }
}

应用

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值