处理@Around通知中是否包含@RequestBody的请求

问题描述 投票:0回答:1

我有这样的基于方面的日志记录:

@Pointcut("@annotation(Loggable)")
public void loggableAnnotation() {}

@Around("loggableAnnotation()")
public Object simpleProcess(ProceedingJoinPoint joinPoint) throws Throwable {
  return this.processWithBody(joinPoint, null);
}

@Around("loggableAnnotation() && args(@org.springframework.web.bind.annotation.RequestBody body,..)")
public Object processWithBody(ProceedingJoinPoint joinPoint, Object body) throws Throwable {
  // do things
}

并且当我用@RequestBody执行请求时,它工作正常,建议processWithBody()被触发。但是,当我执行没有@RequestBody(仅@PathVariable@RequestParam)的请求时,不会触发simpleProcess(),而是在processWithBody()中接收路径变量值作为body参数。

为什么会发生,我如何以不同的方式处理两种类型的请求(如果可能,在相同的建议中?)>

我有这样的基于方面的日志记录:@Pointcut(“ @ annotation(Loggable)”)public void loggableAnnotation(){} @Around(“ loggableAnnotation()”)public Object simpleProcess(ProceedingJoinPoint joinPoint)...

spring spring-boot aspectj spring-aop
1个回答
1
投票

您正在制造三个基本错误:

  • 您正在尝试从args()内部匹配参数自变量注解,但没有任何效果,这就是processWithBody(..)匹配不需要的参数并将其绑定到body的原因。应该将其传输到execution()切入点。

© www.soinside.com 2019 - 2024. All rights reserved.