Skip to content

Commit

Permalink
fix 支持AOP+自定义参数注解读取参数值
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhengjiaao committed Jan 18, 2024
1 parent 578d514 commit 21a5226
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 4 deletions.
15 changes: 15 additions & 0 deletions starter-aop/src/main/java/com/zja/aop/MyBodyParam.java
Original file line number Diff line number Diff line change
@@ -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 {
}
15 changes: 15 additions & 0 deletions starter-aop/src/main/java/com/zja/aop/MyParam.java
Original file line number Diff line number Diff line change
@@ -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 {
}
101 changes: 101 additions & 0 deletions starter-aop/src/main/java/com/zja/aop/ParameterLoggingAspect.java
Original file line number Diff line number Diff line change
@@ -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();
}*/
}
18 changes: 14 additions & 4 deletions starter-aop/src/main/java/com/zja/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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);
}
}
3 changes: 3 additions & 0 deletions starter-aop/src/main/java/com/zja/dto/UserDTO.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 21a5226

Please sign in to comment.