Spring AOP实现打印HTTP接口出入参日志
前言
最近在维护一个运营端的系统,和前端联调的过程中,经常需要排查一些交互上的问题,每次都得看前端代码的传参和后端代码的出参,于是打算给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和出入参了:

说明我们的程序是正确的了。
到此这篇关于Spring AOP实现打印HTTP接口出入参日志的文章就介绍到这了,更多相关Spring AOP打印日志内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Spring Boot + Vue 基于 RSA 的用户身份
RSA是一种非对称加密算法,适用于用户身份认证加密,本文介绍了基于RSA的用户身份认证加密机制的实现,包括前端Vue.js使用jsencrypt库对用户名密码进行加密,后端使用RSA私钥解密验证用户凭据,感兴趣的朋友跟随小编一起看看吧2024-11-11
form-data与x-www-form-urlencoded的区别以及知识延伸
这篇文章主要给大家介绍了关于form-data与x-www-form-urlencoded的区别以及知识延伸,form-data和x-www-form-urlencoded都是HTTP请求中用于传输表单数据的编码格式,需要的朋友可以参考下2023-11-11
java.sql.SQLRecoverableException关闭的连接异常问题及解决办法
当数据库连接池中的连接被创建而长时间不使用的情况下,该连接会自动回收并失效,就导致客户端程序报“ java.sql.SQLException: Io 异常: Connection reset” 或“java.sql.SQLException 关闭的连接”异常问题,下面给大家分享解决方案,一起看看吧2024-03-03
SpringMVC配置多个properties文件之通配符解析
这篇文章主要介绍了SpringMVC配置多个properties文件之通配符解析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-09-09


最新评论