使用Spring的AOP打印HTTP接口出入参日志

开发 前端
本文分享了通过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接口的出入参日志的打印的方法,同时也说明,越是基础的东西,越是实用。

责任编辑:武晓燕 来源: 今日头条
相关推荐

2021-03-01 23:26:41

日志Spring BootAOP

2022-02-08 17:07:54

Spring BooSpring Aop日志记录

2023-03-30 07:48:46

接口鉴权SpringBoot

2020-07-21 08:06:05

日志

2009-06-19 11:09:27

Spring AOP

2012-07-11 14:31:16

SpringAop

2024-03-11 09:32:55

WebClientSpringHTTP

2023-03-16 08:14:57

2024-03-08 10:05:09

SpringHTTP接口

2022-06-07 07:58:45

SpringSpring AOP

2009-06-19 13:28:30

Spring AOPSpring 2.0

2022-02-17 13:39:09

AOP接口方式

2009-06-22 10:41:34

Spring.AOP

2021-11-24 07:25:47

ESClickHouse存储

2009-06-18 14:54:52

Spring AOP

2019-11-29 16:21:22

Spring框架集成

2022-06-08 08:04:28

Springservicerepository

2023-02-01 09:15:41

2009-09-29 10:00:40

Spring AOP框

2023-09-04 08:36:19

SpringAop日志输出
点赞
收藏

51CTO技术栈公众号