接口日志表
id | int | |
---|---|---|
request_url | varchar | 请求url |
method | varchar | 请求方式 |
request_time | varchar | 接口响应时间 |
params | text | 请求参数 |
ip | varchar | 客户端IP |
operation_desc | varchar | 操作描述 |
create_date | datetime | 创建时间 |
环绕通知
在实际开发中应用非常广泛,所以下面用环绕通知
去实现。
创建注解
/anotation/Log.java
// 这个注解用来对controller方法进行描述
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Log {
// 来一个属性,描述这个方法是用来干嘛的
String desc();
}
在接口上加注解
controller/StudentController.java
@GetMapping
@Log(desc = "这是学生列表接口")
public List<Student> list() {
...
}
切面类
/aop/LogAspect.java
// @Pointcut用来标识切点
// RestfulStudentController.*(),指RestfulStudentController里面的所有方法
// @Pointcut("execution(* com.ca.controller.StudentController.*())")
@Pointcut("execution(* com.ca.controller.StudentController.*(..))") // 所有方法的所有参数.*(..)
public void pointcut() {
}
// 添加常量
private static final String title="SpringMvc action report -----";
// 用ThreadLocal来获取时间,new一个ThreadLocal后在里面初始化SimpleDateFormat对象
/*
ThreadLocal操作值的时候是取得当前线程的ThreadLocalMap对象,然后把值设置到了这个对象中,这样对于不同的线程得到的就是不同
的ThreadLocalMap,那么向其中保存值或者修改值都只是会影响到当前线程,这样就保证了线程安全。
*/
private static final ThreadLocal<SimpleDateFormat> sdf = new ThreadLocal() {
@Override
protected