Spring AOP实现打印HTTP接口出入参日志
作者:IT小村 发布时间:2021-10-09 13:38:37
标签:Spring,AOP,日志
前言
最近在维护一个运营端的系统,和前端联调的过程中,经常需要排查一些交互上的问题,每次都得看前端代码的传参和后端代码的出参,于是打算给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


猜你喜欢
- 介绍使用mybatis时可以使用二级缓存提高查询速度,进而改善用户体验。使用redis做mybatis的二级缓存可是内存可控<如将单独
- activity_main.xml在res/layout文件中,放置一个TextView控件用于显示购物商城界面的标题,放置一个ListVi
- 做Java的面试题时遇到了以下这题,百度了一下Math.round()的修约规则,有的说是四舍五入,有的说是四舍六入,发现和我学分析化学时用
- 这段C#代码主要是验证身份证的开头和身份证的格式和长度是否正确,没有按照身份证的编码规则进行严格验证/// <summary>
- 想做一个APP,设计中有侧边栏这个功能,所以现在开始学习下侧边栏的实现。在官方的UI空间中已经给出了DrawerLayout这个侧滑的菜单空
- 本文实例为大家分享了Java实现生成n个不重复的随机数的具体代码,供大家参考,具体内容如下需求:根据min和max,生成n个不重复的随机数。
- 本文实例分析了Android编程之TextView的字符过滤功能。分享给大家供大家参考,具体如下:TextView可以设置接受各式各样的字符
- 使用linq把多列的List转化为只有指定列的List1、方式一2、方式二
- 布局中listview要覆盖标题栏 int mTouchSlop = ViewConfiguration.get(this).getScal
- 一、系统启动后注入配置package com.example.config;import org.springframework.beans
- 一,概念1,排序排序,就是使一串记录,按照其中的某个或某些关键字的大小,递增或递减的排列起来的操作。 平时的上下文中,如果提到排序,通常指的
- 本文实例讲述了C#实现闪动托盘图标效果的方法。分享给大家供大家参考,具体如下:在用户正在登录QQ或者使用Firemail邮件系统自动收取邮件
- 1.user实体package com.demo.dto;public class User { private Integer
- 1. 通过设置采样率压缩res资源图片压缩 decodeResource public Bitmap decodeSampled
- Quartz是一款开源的定时任务调度框架,Quartz的官网是:http://www.quartz-s
- 有些时候我们做的程序需要进度条,而vs提供的控件不是我们想要的。先看效果图:进度条闪烁动画,当然背景可设为Transparent之前想手绘进
- 读取resources下文件的方法网上有问答如下:问:new FileInputStream("src/main/resource
- 本文实例为大家分享了C#实现简单的计算器功能的具体代码,供大家参考,具体内容如下环境:VS2010及以上版本1、建立个Window窗体应用2
- Android init.rc文件详解本文主要来自$ANDROID_SOURCE/system/init/readme.txt的翻译.1 简
- java通过IP解析地理位置在项目开发中,需要在登录日志或者操作日志中记录客户端ip所在的地理位置。目前根据ip定位地理位置的第三方api有