zoukankan      html  css  js  c++  java
  • 工作中实现记录接口调用的注解例子

    本例编写了一个作用于方法之上的自定义注解,主要作用是记录方法的入参以及返回结果

    1.自定义一个注解

    /**
     * 需要切的方法
     */
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    @Documented
    public @interface RequestLogAnnotation {
    
        // 操作的业务类型
        public String value() default "";
    
    }

    2.注解定义好了,我们就需要编写一个能处理上面注解的处理类

    
    
    @Aspect
    @Component
    public class RequestLogAspect {

    public final static Logger logger = LoggerFactory.getLogger(RequestLogAspect.class);

    @Pointcut("@annotation(com.***.***.***.service.aspect.RequestLogAnnotation)")//上面注解的包路径
    public void requestLogPointcut() {
    }


    @Autowired
    private PerfRequestLogMapper perfRequestLogMapper; //自定义的一个dao

    @Around("requestLogPointcut()")
    public Object process(ProceedingJoinPoint point) {
    MethodSignature methodSignature = (MethodSignature) point.getSignature();
    Method method = methodSignature.getMethod();
    String requestParam = logRequestParam(point);
    PerfRequestLogDO perfRequestLogDO = new PerfRequestLogDO();
    perfRequestLogDO.setRequestParam(requestParam);
    perfRequestLogDO.setRequestMethod(method.getName());
    Object result = null;
    try {
    result = point.proceed();
    if (Objects.isNull(result)){
    return result;
    }
    perfRequestLogDO.setResponseResult(JSON.toJSONString(result));
    perfRequestLogMapper.insertSelective(perfRequestLogDO);
    } catch (Exception e) {
    logger.error("切面处理失败_Exception:{}", e);
    } catch (Throwable e) {
    logger.error("切面处理失败_Exception:{}", e);
    }
    return result;
    }

    private static String logRequestParam(ProceedingJoinPoint joinPoint) {
    Object[] args = joinPoint.getArgs();
    StringBuilder arguments = new StringBuilder();
    for (Object arg : args) {
    arguments.append(arg.getClass().getSimpleName());
    if (arg == null) {
    continue;
    }
    arguments.append("=").append(Objects.isNull(arg) ? "NULL" : arg.toString());
    }
    return arguments.toString();
    }
    }
     

    3.入库dao

     <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.**.**.**.dao.saleperf.entity.PerfRequestLogDO" useGeneratedKeys="true">
            insert into perf_request_log
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="requestMethod != null">
                    request_method,
                </if>
                <if test="requestParam != null">
                    request_param,
                </if>
                <if test="responseResult != null">
                    response_result,
                </if>
                <if test="erroMessage != null">
                    erro_message,
                </if>
            </trim>
            <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="requestMethod != null">
                    #{requestMethod,jdbcType=VARCHAR},
                </if>
                <if test="requestParam != null">
                    #{requestParam,jdbcType=VARCHAR},
                </if>
                <if test="responseResult != null">
                    #{responseResult,jdbcType=VARCHAR},
                </if>
                <if test="erroMessage != null">
                    #{erroMessage,jdbcType=VARCHAR},
                </if>
            </trim>
        </insert>
  • 相关阅读:
    win7常用快捷键
    java中构造代码块、方法调用顺序问题
    eclipse项目改为maven项目导致svn无法比较历史数据的解决办法
    linux配置Anaconda python集成环境
    DataFrame对行列的基本操作实战
    驱动:电阻屏触摸芯片NS2009
    读书笔记:代码大全(第二版)
    资料:磁角度传感器芯片
    经验:FatFs文件系统实时写入
    笔记:CAN收发器-TJA1051T与TJA1051T/3调试总结
  • 原文地址:https://www.cnblogs.com/UncleWang001/p/12454443.html
Copyright © 2011-2022 走看看