开始aop demo测试
以自定义注解的方式,决定哪些接口要进行feign拦截
1.自定义注解 SendBankAnnotation,对使用改注解的feign接口进行拦截
@Target({ElemenType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SendBankAnnotation{
//接口标识
String value();
//提示语
String notes() default "";
}
2.feign远程调用
@FeignClient(name="custorm",fallback=Hysitx.class)
public interface IRemoteCallService {
@RequestMapping(value="/custorm/getTest",method = RequestMethod.POST)
@SendBankAnnotation(value = "自定义接口名称",notes= “备注”)
List<String> test(BaseReq req);
}
3.aop拦截
@Aspect
@Component
public class sendFilter{
private Loggen logger = TSLogFactory.get(sendFilter.class);
@Pointcut("@annotation(com....自定义注解的路径)")
private void execute(){}
@around("execute()")
public Object around(ProceedingJoinPoint pjp){
long beginTime = System.currentTimeMillis();
Object result = null;
String edspTransCode = "";//交易接口
try{
//对请求的参数进行转换
Object[] paramValues = pjp.getArgs();//获取参数数组
Signature sig = pjp.getSignature();//获取方法对象
MethodSignature msig = (MethodSignature )sig;
Method method = msig.getMethod();
//获取自定义注解的属性值
SendBankAnnotation ann = method.getAnnotation(SendBankAnnotation.class);
edspTransCode = ann.value();//自定义注解的 接口名称
if("要过滤的接口名".equals(edspTransCode )){
BaseReq req = null;
Object arg0 = pjp.getArgs()[0];//接收入参
if(arg0 instanceof BaseReq){
req = (BaseReq).arg0;
}
}
transformDictArgs(edspTransCode,pjp.getArgs());//抽取公共方法,对入参进行转换
logger.inf("接口变化:+edsptranscode+",参数"+JSON.tpJSONString(paramValues ));
result = pjp.preceed();
//此时对返回的结果做参数转换
transformDictResult(edspTransCode,result);
}finally{
long costTime = System.currenTimeMillis()-beginTime;
logger.info("接口编号:"+edspTransCode +"耗时:"+costTime+",返回值:"+JSON.tpJSONString(result ));
}
}
}