日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

aop springboot 传入参数_Springboot添加AOP打印请求参数

發(fā)布時間:2023/12/4 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 aop springboot 传入参数_Springboot添加AOP打印请求参数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 引入依賴

org.springframework.boot

spring-boot-starter-aop

2. 寫切面

切面類需要加@Aspect和@Component注解

package com.test.demo.aspect;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.AfterReturning;

import org.aspectj.lang.annotation.Around;

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;

import cn.hutool.core.date.DateUtil;

import cn.hutool.extra.servlet.ServletUtil;

import cn.hutool.json.JSONUtil;

import io.swagger.annotations.Api;

import lombok.extern.slf4j.Slf4j;

@Aspect

@Component

@Slf4j

public class LogPrintAspect {

@Pointcut("execution(public * com.test.demo.*.*(..))")

public void getMethodName() {

}

@Pointcut("within(com.test.demo..*) && @within(org.springframework.web.bind.annotation.RestController)")

public void printParams() {

}

/**

* 打印請求參數

* @param joinPoint

*/

@Before(value = "printParams()")

public void paramsLog(JoinPoint joinPoint) {

ServletRequestAttributes requestAttributes =

(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

HttpServletRequest request = requestAttributes.getRequest();

Class> targetClass = joinPoint.getTarget().getClass();

if (targetClass.isAnnotationPresent(Api.class)) {

Api swaggerApi = (Api) targetClass.getDeclaredAnnotation(Api.class);

log.info("模塊的中文漢字 {} , {}", swaggerApi.value(), swaggerApi.tags()[0]);

}

log.info("sign -- {}", joinPoint.getSignature());

log.info("請求地址:" + request.getRequestURL().toString());

log.info("請求方式:" + request.getMethod());

Map m = ServletUtil.getParamMap(request);

log.info("請求參數:{}", m);

}

/**

* 打印響應內容

* @param o

*/

@AfterReturning(returning = "o", pointcut = "printParams()")

public void methodAfterReturing(Object o) {

log.info(JSONUtil.parse(o).toJSONString(1));

}

/**

* 打印方法耗時

* @param joinPoint

* @return

* @throws Throwable

*/

@Around("printParams()")

public Object around(ProceedingJoinPoint joinPoint) throws Throwable{

long startTime = DateUtil.current(false);

log.info("執(zhí)行方法:{},開始時間:{}" , joinPoint.getSignature().getName() , startTime);

Object result = joinPoint.proceed();

long endTime= DateUtil.current(false);

log.info("執(zhí)行方法:{},結束時間:{} , 用時:{} 毫秒 , " , joinPoint.getSignature().getName() , endTime, endTime-startTime);

return result ;

}

}

總結

以上是生活随笔為你收集整理的aop springboot 传入参数_Springboot添加AOP打印请求参数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。