1. 程式人生 > 其它 >AOP實現註解,改變Controller返回引數

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 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"
}