Spring AOP实现打印HTTP接口出入参日志
作者:IT小村 时间:2021-10-09 13:38:37
前言
最近在维护一个运营端的系统,和前端联调的过程中,经常需要排查一些交互上的问题,每次都得看前端代码的传参和后端代码的出参,于是打算给HTTP接口加上出入参日志。
但看着目前的HTTP接口有点多,那么有什么快捷的方式呢?答案就是实用Spring的AOP功能,简单实用。
思路
定义个一个SpringAOP的配置类,里边获取请求的URL、请求的入参、相应的出参,通过日志打印出来。
SpringBoot的aop依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
示例
1.编写一个HTTP接口
定义了一个Controller,里边就一个方法,方法请求类型是get,出入参都是简单的一个字符串字段。
package com.example.springbootaoplog.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author hongcunlin
*/
@RestController
@RequestMapping("/index")
public class IndexController {
@GetMapping("/indexContent")
public String indexContent(String param) {
return "test";
}
}
2.编写一个AOP日志配置
这算是本文的重点了,定义一个AOP的内容,首先是切点,再者是请求前日志打印,最后请求后日志打印
package com.example.springbootaoplog.config;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
/**
* aop日志打印配置
*
* @author hongcunlin
*/
@Slf4j
@Aspect
@Component
public class AopLogConfig {
/**
* 切点路径:Controller层的所有方法
*/
@Pointcut("execution(public * com.example.springbootaoplog.controller.*.*(..))")
public void methodPath() {
}
/**
* 入参
*
* @param joinPoint 切点
*/
@Before(value = "methodPath()")
public void before(JoinPoint joinPoint) {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
String url = requestAttributes.getRequest().getRequestURL().toString();
log.info("请求 = {}, 入参 = {}", url, JSON.toJSONString(joinPoint.getArgs()));
}
/**
* 出参
*
* @param res 返回
*/
@AfterReturning(returning = "res", pointcut = "methodPath()")
public void after(Object res) {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
String url = requestAttributes.getRequest().getRequestURL().toString();
log.info("请求 = {}, 入参 = {}", url, JSON.toJSONString(res));
}
}
3.结果测试
我们通过浏览器的URL,针对我们编写的http接口,发起一次get请求:
可以看到,日志里边打印了我们预期的请求的URL和出入参了:
说明我们的程序是正确的了。
来源:https://www.51cto.com/article/719612.html
标签:Spring,AOP,日志
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
spring profile 多环境配置管理详解
2023-01-23 17:53:58
Java接口的简单定义与实现方法示例
2022-11-22 05:04:43
java抓取网页或文件中的邮箱号码
2023-07-30 19:19:28
![](https://img.aspxhome.com/file/2023/9/62119_0s.jpg)
分布式Netty源码分析EventLoopGroup及介绍
2022-02-12 03:20:51
![](https://img.aspxhome.com/file/2023/9/70289_0s.png)
Java Web实现添加定时任务的方法示例
2023-11-25 02:12:15
spring boot 枚举使用的坑整理
2022-09-17 09:03:15
java线程之join方法的使用介绍
2023-01-15 12:18:40
Spring Boot+Drools规则引擎整合详解
2023-11-28 20:33:00
java寻找迷宫路径的简单实现示例
2021-07-06 13:17:50
Flutter 剪裁组件的使用
2023-06-18 13:15:04
![](https://img.aspxhome.com/file/2023/3/96783_0s.png)
.net的序列化与反序列化实例
2022-12-29 00:50:50
Java实现驼峰和下划线互相转换的示例代码
2023-12-16 15:23:48
![](https://img.aspxhome.com/file/2023/6/61066_0s.png)
java实现udp通讯的代码
2022-07-18 13:28:52
JAVA ArrayList详细介绍(示例)
2023-02-18 22:30:56
![](https://img.aspxhome.com/file/2023/3/61143_0s.jpg)
Java 详解循环屏障CyclicBarrier如何实现多线程分段等待执行完成
2022-02-16 04:50:49
struts2中使用注解配置Action方法详解
2023-08-30 00:01:25
Android 实现可任意拖动的悬浮窗功能(类似悬浮球)
2023-08-07 10:19:05
SpringCloud URL重定向及转发代码实例
2023-11-19 11:43:18
java动态导出excel压缩成zip下载的方法
2022-07-13 07:29:27
Spring AOP实现权限检查的功能
2023-08-10 06:51:14
![](https://img.aspxhome.com/file/2023/5/69285_0s.png)