最近在项目里面需要对一些controller接口进行记录。记录的信息包括,接口的耗时、请求参数、请求url、是否成功、请求的ip地址。
最后利用aop切面编程。
@Order(1)
@Aspect
@Component
@Slf4j
public class BehaviorAop {@AutowiredBehaviorService 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 apiOperation = method.getAnnotation(ApiOperation.class);//记录接口名字if (EmptyUtils.isNotEmpty(apiOperation) && EmptyUtils.isNotEmpty(apiOperation.value())) {behaviorRecordMo.setName(apiOperation.value());}Enumeration<String> attributeNames = request.getAttributeNames();behaviorRecordMo.setAppName("app-name");behaviorRecordMo.setUserId(request.getHeader(AdminHeaderEnum.adminId.name()));behaviorRecordMo.setIp(WebUtils.getIp(request));behaviorRecordMo.setCreateTime(LocalDateTime.now());behaviorRecordMo.setRefererUrl(request.getHeader(WxHeaderEnum.Referer.name()));behaviorRecordMo.setRefererName(request.getHeader(WxHeaderEnum.Referer.name()));behaviorRecordMo.setUrl(request.getRequestURI());behaviorRecordMo.setMethod(request.getMethod());//获取接口的请求参数behaviorRecordMo.setParam(argsToString(joinPoint.getArgs()));watch.start();//开始执行具体的业务方法Object result = joinPoint.proceed();//业务方法执行结束watch.stop();log.debug("目标方法返回结果后...");ResultBean resultBean = null;try {resultBean = (ResultBean) result;behaviorRecordMo.setStatus(resultBean.getStatus());behaviorRecordMo.setMsg(resultBean.getMsg());} catch (Exception e) {behaviorRecordMo.setStatus(ResultStatusEnum.SUCCESS.getStatus());}behaviorRecordMo.setTime(watch.getTime());behaviorService.insert(behaviorRecordMo);return result;} catch (Exception e) {log.debug("执行目标方法异常后...");watch.stop();behaviorRecordMo.setStatus(ResultStatusEnum.FAIL.getStatus());behaviorRecordMo.setMsg(e.getMessage());behaviorRecordMo.setTime(watch.getTime());behaviorService.insert(behaviorRecordMo);//异常通知throw e;}}private String argsToString(Object[] args) {StringBuffer stringBuffer = new StringBuffer();if (EmptyUtils.isNotEmpty(args)) {for (Object object : args) {if (object instanceof HttpServletRequest) {stringBuffer.append("HttpServletRequest" + ";\n");} else if (object instanceof HttpServletResponse) {stringBuffer.append("HttpServletResponse" + ";\n");} else if (object instanceof MultipartFile) {stringBuffer.append("MultipartFile" + ";\n");} else {stringBuffer.append(JsonLUtils.toJSon(object) + ";\n");}}}return stringBuffer.toString();}
}
然后我们在controller里面对一些方法加上@ApiOperation这个注解
@ApiOperation(value = "统计在线顾问人数", notes = "统计在线顾问人数")@GetMapping("/count/agent")public ResultBean<List<List<AgentOfflineAreaCountVo>>> offlineCountDetail(@RequestParam String svOrderId) {List<List<AgentOfflineAreaCountVo>> agentOnlineCountMap = this.agentOfflineService.offlineCountDetail(svOrderId);return super.success(agentOnlineCountMap);}