SpringBoot-AOP-获取@RequestBody值

// 获取请求参数进行打印 ================
Signature signature1 = joinPoint.getSignature();
JSONObject signatureJson = JSON.parseObject(JSON.toJSONString(signature1));
JSONArray parameterNames = signatureJson.getJSONArray("parameterNames");
List<Object> argList = new ArrayList<>();
for (Object arg : joinPoint.getArgs()) {
	// request/response无法使用toJSON
	if (arg instanceof HttpServletRequest) {
		argList.add("request");
	} else if (arg instanceof HttpServletResponse) {
		argList.add("response");
	} else {
		argList.add(JSON.toJSON(arg));
	}
}
operlog.setOperRequParam(argList.toString());

最后把数组的值塞进去、有些报错可能是参数太多,超长了。

先判断是否为空,我的数据库设计的是600,但有时候会超过一点,不知道为啥。

判断一下是否超过512,如果超就去裁剪,不判断直接去裁剪,可能会裁剪不到512会报错。

String body = operationLog.getOperRequParam();
if (body != null){
    if (operationLog.getOperRequParam().length() > 512){
        operationLog.setOperRequParam(body.substring(0,512));
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在AOP获取`@RequestBody`的信息,可以使用`JoinPoint`参数和`MethodSignature`对象来获取方法的参数信息。然后,通过遍历参数注解数组,找到带有`@RequestBody`注解的参数,并获取相应的信息。 以下是一个示例代码: ```java @Aspect @Component public class RequestBodyAspect { @Before("execution(* com.example.controller.*.*(..))") public void logRequestBody(JoinPoint joinPoint) throws NoSuchMethodException { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); String[] parameterNames = signature.getParameterNames(); Annotation[][] parameterAnnotations = signature.getMethod().getParameterAnnotations(); for (int i = 0; i < parameterAnnotations.length; i++) { for (Annotation annotation : parameterAnnotations[i]) { if (annotation instanceof RequestBody) { System.out.println("RequestBody parameter name: " + parameterNames[i]); // 获取参数类型 Class<?> parameterType = signature.getMethod().getParameterTypes()[i]; System.out.println("RequestBody parameter type: " + parameterType); // 其他操作... } } } } } ``` 在上述示例中,切面类`RequestBodyAspect`使用`@Before`注解标记了一个方法`logRequestBody`,该方法会在目标方法执行前执行。通过遍历参数注解数组,找到带有`@RequestBody`注解的参数,并获取参数的名称和类型。 请注意,由于`@RequestBody`注解通常用于接收请求体中的JSON或XML等数据,因此参数类型可能是自定义的POJO类或其他类型。您可以根据实际需要进行进一步操作,例如将请求体转换为对象或执行其他逻辑处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小巫医初春

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值