注解实现类 : InterfaceLogAspect :
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fintell.dp3.api.async.AsyncTask;
import lombok.extern.slf4j.Slf4j;
@Aspect
@Component
@Order(-10)
@Slf4j
public class InterfaceLogAspect {
private static final ThreadLocal<Long> threadLocal = new ThreadLocal<Long>();
@Before("@annotation(InterfaceLog)")
public void before() {
// 在threadlocal设置时间
threadLocal.set(System.currentTimeMillis());
}
@AfterReturning(returning = "o", pointcut = "@annotation(InterfaceLog)")
public void after(JoinPoint joinPoint, Object o) {
try {
// 获取到注解
InterfaceLog annotation = ((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(InterfaceLog.class);
// 获取参数
JSONObject jsonObject = JSONObject.parseObject(JSON.toJSON(o).toString());
// 获取threadlocal参数
threadLocal.get()
// 获取注解数据
annotation.edition()
// 获取到方法名称
joinPoint.getSignature().getName()
} catch (Exception e) {
log.error("error : {} . ", e);
} finally {
// 移除threadlocal
threadLocal.remove();
}
}
}
注解类 InterfaceLog :
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface InterfaceLog {
int edition();
}
使用 controller 方法 :
@InterfaceLog(edition = 1)
public ApiResponseVo report(@RequestBody ApiRequestVo apiRequestVo) {
// ......
}