最近在项目里面需要对一些controller接口进行记录。记录的信息包括,接口的耗时、请求参数、请求url、是否成功、请求的ip地址。
最后利用aop切面编程。
@Order(1)
@Aspect
@Component
@Slf4j
public class BehaviorAop {
@Autowired
BehaviorService behaviorService;
/*
* 我们对ApiOperation 这个注解进行切面,也就是说controller里面的方法只要被这个注解修饰,那么这个方法都会被记录详细信息,ApiOperation 这个注解是swagger的注解,当然我们也能自定义注解。
/
@Pointcut("@annotation(io.swagger.annotations.ApiOperation)")
public void annotationTo() {
}
@Around("annotationTo()")
public Object aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable {
BehaviorRecordMo behaviorRecordMo = new BehaviorRecordMo();
StopWatch watch = new StopWatch();
try {
log.debug("目标方法执行前...");
//通过切面获取当前执行的方法
Object target = joinPoint.getTarget();
Signature sig = joinPoint.getSignature();
MethodSignature msig = (MethodSignature) sig;
Method method = target.getClass().getMethod(msig.getName(), msig.getParameterTypes());
ApiOperation api