ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

AOP实现注解,改变Controller返回参数

2021-11-08 17:04:49  阅读:134  来源: 互联网

标签:Map AOP response Controller result import moduleValue 注解 annotation


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

一、自定义注解

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 extends BaseService {

//    @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)) {
                return 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(@RequestBody EsgSearchDto esgSearchDto) {
        Map<String, Object> response = new HashMap<>(2);
        response.put("result", "result");
        return response;
    }

返回结果:

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

返回结果

{
  "result": "result"
}

 

标签:Map,AOP,response,Controller,result,import,moduleValue,注解,annotation
来源: https://www.cnblogs.com/LiuFqiang/p/15524966.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有