zoukankan      html  css  js  c++  java
  • AOP实现注解,改变Controller返回参数

    需要实现当前接口是否为付费版本,如果不是付费版本,修改返回的参数

    一、自定义注解

    import org.springframework.core.annotation.Order;
    
    import java.lang.annotation.*;
    
    /**
     * 独立收费版本 付费模块
     * @author lfq
     */
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Order(1)
    public @interface PayModule {
    
        /**
         * 模块名
         */
        String moduleName();
    
        /**
         * 模块对应码值 
         */
        String moduleValue();
    }

    二、创建切片类

    import com.stock.capital.services.announcement.common.annotation.PayModule;
    import com.stock.capital.services.announcement.common.dao.PayModuleMapper;
    import com.stock.core.dto.JsonResponse;
    
    import com.stock.core.service.BaseService;
    import org.apache.commons.lang3.StringUtils;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
    import org.aspectj.lang.reflect.MethodSignature;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    import java.lang.reflect.Method;
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * @author lfq
     */
    @Component
    @Aspect
    public class PayModuleAspect {
    
    //    @Autowired
    //    private PayModuleMapper payModuleMapper;
    
        @Pointcut("@annotation(com.stock.capital.services.announcement.common.annotation.PayModule)")
        public void annotationCallTime() {
        }
    
    //    @Around(value = "annotationCallTime()")
    //    public JsonResponse doAround(ProceedingJoinPoint joinPoint) {
    //        MethodSignature ms = (MethodSignature) joinPoint.getSignature();
    //        Method method = ms.getMethod();
    //        PayModule annotation = method.getAnnotation(PayModule.class);
    //        String moduleValue = annotation.moduleValue();
    //
    //        Map<String, Object> params = new HashMap<>(2);
    //        params.put("companyCode", getUserInfo().getCompanyCode());
    //        params.put("moduleValue", moduleValue);
    //
    //        try {
    //            JsonResponse response = (JsonResponse) joinPoint.proceed();
    //            if (response.isSuccess()) {
    //                Map<String, Object> result = (Map<String, Object>) response.getResult();
    //                Integer payFlay = payModuleMapper.queryPayModule(params);
    //                if (payFlay != null) {
    //                    result.put("isPay", payFlay);
    //                }
    //            }
    //            return response;
    //        } catch (Throwable throwable) {
    //
    //        }
    //        return null;
    //    }
    
        @Around(value = "annotationCallTime()")
        public Object doAround(ProceedingJoinPoint joinPoint) {
            MethodSignature ms = (MethodSignature) joinPoint.getSignature();
            Method method = ms.getMethod();
            PayModule annotation = method.getAnnotation(PayModule.class);
            String moduleValue = annotation.moduleValue();
    
            try {
                Map<String, Object> response = (Map<String, Object>) joinPoint.proceed();
                if (StringUtils.equals("0013", moduleValue)) {
                    response.put("isPay", "1");
                }
                
                return response;
            } catch (Throwable throwable) {
    
            }
            return null;
        }
    }

    3、测试返回结果

        @RequestMapping(value = "/queryDetailInfo", method = RequestMethod.POST)
        @ResponseBody
        @PayModule(moduleName = "", moduleValue = "0013")
        public Map<String, Object> queryDetailInfo() {
            Map<String, Object> response = new HashMap<>(2);
            response.put("result", "result");
            return response;
        }

    返回结果:

    {
      "result": "result",
      "isPay": "1"
    }
     @RequestMapping(value = "/queryDetailInfo", method = RequestMethod.GET)
        @ResponseBody
        @PayModule(moduleName = "", moduleValue = "0011")
        public Map<String, Object> queryDetailInfo() {
            Map<String, Object> response = new HashMap<>(2);
            response.put("result", "result");
            return response;
        }

    返回结果

    {
      "result": "result"
    }

     

  • 相关阅读:
    spring-framework-x.x.x.RELEASE-dist下载教程
    SpringMVC——SpringMVC简介
    SSM框架报错分析(一)——There is no getter for property named 'XXX' in 'class java.lang.String'
    mysql一次查询,返回多个统计结果
    Mybatis进阶学习笔记——关系查询——一对多查询
    Mybatis进阶学习笔记——关系查询——一对一查询
    Mybatis进阶学习笔记——动态sql
    Mybatis进阶学习笔记——输出映射
    C#语言概述
    第一个C#程序Hello World
  • 原文地址:https://www.cnblogs.com/LiuFqiang/p/15524966.html
Copyright © 2011-2022 走看看