1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.log.AspectLog;
import com.yanzuoguang.log.LogInfoVo;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.vo.ResponseResult;
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.springframework.stereotype.Component;
/**
* 接口切面,用于验证接口回调返回参数
*
* @author 颜佐光
*/
@Aspect
@Component
public class AspectFeign {
private static final String TAG = AspectFeign.class.getSimpleName();
private final AspectLogUrl aspectLogUrl;
private final AspectLogBody aspectLogBody;
private final AspectLog aspectLog;
public AspectFeign(AspectLogUrl aspectLogUrl,
AspectLogBody aspectLogBody,
AspectLog aspectLog) {
this.aspectLogUrl = aspectLogUrl;
this.aspectLogBody = aspectLogBody;
this.aspectLog = aspectLog;
}
/**
* AOP的表达式
*/
@Pointcut("execution(* *..feign..*Feign.*(..))")
public void feignAspect() {
}
/**
* 执行环形切面
*
* @param joinPoint 切面对象
* @return 切面结果
*/
@Around(value = "feignAspect()")
public Object requestFeignAround(ProceedingJoinPoint joinPoint) throws Throwable {
Class<?> declaringType = joinPoint.getSignature().getDeclaringType();
String url = aspectLogUrl.getWebMethodUrl(joinPoint);
Object requestBody = aspectLogBody.getRequestBody(joinPoint);
boolean clear = aspectLog.requestLogInit();
LogInfoVo log = aspectLog.start(declaringType, TAG, url, requestBody, clear);
Object result = null;
Exception ex = null;
try {
result = joinPoint.proceed();
// 假如是标准格式,则验证接口是否成功,不成功则抛出异常
if (result instanceof ResponseResult) {
ResponseResult<?> responseResult = (ResponseResult<?>) result;
responseResult.check();
}
return result;
} catch (CodeException e) {
ex = e;
throw e;
} catch (Exception e) {
result = ExceptionHelper.getError(e);
ex = e;
throw e;
} finally {
aspectLog.result(log, result, ex);
}
}
}