From 21a5226f742292c270306e198f24fbc59136f10f Mon Sep 17 00:00:00 2001 From: Zhengjiaao Date: Thu, 18 Jan 2024 13:53:44 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E6=94=AF=E6=8C=81AOP+=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E5=8F=82=E6=95=B0=E6=B3=A8=E8=A7=A3=E8=AF=BB=E5=8F=96?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/zja/aop/MyBodyParam.java | 15 +++ .../src/main/java/com/zja/aop/MyParam.java | 15 +++ .../com/zja/aop/ParameterLoggingAspect.java | 101 ++++++++++++++++++ .../com/zja/controller/UserController.java | 18 +++- .../src/main/java/com/zja/dto/UserDTO.java | 3 + 5 files changed, 148 insertions(+), 4 deletions(-) create mode 100644 starter-aop/src/main/java/com/zja/aop/MyBodyParam.java create mode 100644 starter-aop/src/main/java/com/zja/aop/MyParam.java create mode 100644 starter-aop/src/main/java/com/zja/aop/ParameterLoggingAspect.java diff --git a/starter-aop/src/main/java/com/zja/aop/MyBodyParam.java b/starter-aop/src/main/java/com/zja/aop/MyBodyParam.java new file mode 100644 index 0000000..67438b0 --- /dev/null +++ b/starter-aop/src/main/java/com/zja/aop/MyBodyParam.java @@ -0,0 +1,15 @@ +package com.zja.aop; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * @author: zhengja + * @since: 2024/01/17 17:10 + */ +@Target(ElementType.PARAMETER) +@Retention(RetentionPolicy.RUNTIME) +public @interface MyBodyParam { +} diff --git a/starter-aop/src/main/java/com/zja/aop/MyParam.java b/starter-aop/src/main/java/com/zja/aop/MyParam.java new file mode 100644 index 0000000..2602b98 --- /dev/null +++ b/starter-aop/src/main/java/com/zja/aop/MyParam.java @@ -0,0 +1,15 @@ +package com.zja.aop; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * @author: zhengja + * @since: 2024/01/17 16:41 + */ +@Target({ElementType.PARAMETER, ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface MyParam { +} diff --git a/starter-aop/src/main/java/com/zja/aop/ParameterLoggingAspect.java b/starter-aop/src/main/java/com/zja/aop/ParameterLoggingAspect.java new file mode 100644 index 0000000..b3a3f11 --- /dev/null +++ b/starter-aop/src/main/java/com/zja/aop/ParameterLoggingAspect.java @@ -0,0 +1,101 @@ +package com.zja.aop; + +import com.alibaba.fastjson.JSON; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.reflect.MethodSignature; +import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import javax.servlet.http.HttpServletRequest; +import java.io.BufferedReader; +import java.io.IOException; +import java.lang.annotation.Annotation; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Parameter; + +/** + * @author: zhengja + * @since: 2024/01/17 16:22 + */ +@Aspect +@Component +public class ParameterLoggingAspect { + + @Before("execution(* com.zja.controller.UserController.*(..))") + public void logMethodParameters(JoinPoint joinPoint) throws IllegalAccessException { + MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); + Method method = methodSignature.getMethod(); + Class[] parameterTypes = method.getParameterTypes(); + String[] parameterNames = methodSignature.getParameterNames(); + + for (int i = 0; i < parameterNames.length; i++) { + for (Annotation annotation : method.getParameterAnnotations()[i]) { + if (annotation instanceof MyParam) { + Object myAnnotatedParameter = joinPoint.getArgs()[i]; + + // 在这里处理获取到的参数值 + System.out.println("方法中的字段:"); + System.out.println("参数名称:" + parameterNames[i]); + System.out.println("参数类型:" + parameterTypes[i]); + System.out.println("参数值:" + myAnnotatedParameter); + } + + if (annotation instanceof MyBodyParam) { + Object arg = joinPoint.getArgs()[i]; + if (arg != null) { + Class argClass = arg.getClass(); + Field[] fields = argClass.getDeclaredFields(); + + for (Field field : fields) { + if (field.isAnnotationPresent(MyParam.class)) { + field.setAccessible(true); + Object annotatedFieldValue = field.get(arg); + + // 在这里处理获取到的字段值 + System.out.println("方法中类里面的字段:"); + System.out.println("字段名称:" + field.getName()); + System.out.println("字段类型:" + field.getType()); + System.out.println("字段值:" + annotatedFieldValue); + } + } + } + } + } + } + } + + // todo 不推荐,当getRequestBody() 会遇到问题 : getInputStream() has already been called for this request + /* @Before("execution(* com.zja.controller.UserController.*(..))") + public void logMethodParameters(JoinPoint joinPoint) throws IOException { + ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + HttpServletRequest httpRequest = attributes.getRequest(); + // 获取请求的参数 + String paramValue = httpRequest.getParameter("name"); + if (StringUtils.isEmpty(paramValue)) { + // 获取POST、PUT请求Body的参数 + if (httpRequest.getMethod().equalsIgnoreCase("POST") || httpRequest.getMethod().equalsIgnoreCase("PUT")) { + String requestBody = getRequestBody(httpRequest); + paramValue = JSON.parseObject(requestBody).getString("name"); + // 进行参数的处理或其他操作 + System.out.println("POST or PUT Parameter: " + paramValue); + } + } + // 打印参数值或进行其他操作 + System.out.println("Parameter [name]=: " + paramValue); + } + + private String getRequestBody(HttpServletRequest request) throws IOException { + StringBuilder stringBuilder = new StringBuilder(); + BufferedReader reader = request.getReader(); + String line; + while ((line = reader.readLine()) != null) { + stringBuilder.append(line); + } + return stringBuilder.toString(); + }*/ +} diff --git a/starter-aop/src/main/java/com/zja/controller/UserController.java b/starter-aop/src/main/java/com/zja/controller/UserController.java index 1c339c9..458a5f8 100644 --- a/starter-aop/src/main/java/com/zja/controller/UserController.java +++ b/starter-aop/src/main/java/com/zja/controller/UserController.java @@ -8,11 +8,11 @@ */ package com.zja.controller; +import com.zja.aop.MyBodyParam; +import com.zja.aop.MyParam; +import com.zja.dto.UserDTO; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/aop") @@ -22,4 +22,14 @@ public class UserController { public ResponseEntity queryById(@PathVariable Long id) { return ResponseEntity.ok(id); } + + @PostMapping("/user/v1") + public ResponseEntity queryById(@RequestBody @MyBodyParam UserDTO userDTO) { + return ResponseEntity.ok(userDTO); + } + + @GetMapping("/user/v1") + public ResponseEntity queryById(@RequestParam String id, @RequestParam @MyParam String name) { + return ResponseEntity.ok(id + "=" + name); + } } diff --git a/starter-aop/src/main/java/com/zja/dto/UserDTO.java b/starter-aop/src/main/java/com/zja/dto/UserDTO.java index a18c851..d1bbd90 100644 --- a/starter-aop/src/main/java/com/zja/dto/UserDTO.java +++ b/starter-aop/src/main/java/com/zja/dto/UserDTO.java @@ -1,5 +1,7 @@ package com.zja.dto; +import com.zja.aop.MyBodyParam; +import com.zja.aop.MyParam; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @@ -17,6 +19,7 @@ public class UserDTO implements Serializable { @ApiModelProperty(value = "用户id") private String id; @ApiModelProperty(value = "用户名") + @MyParam private String name; @ApiModelProperty(value = "时间") private Date date;