2017-07-17 44 views
0

我有以下方面hadling所有REST控制器:弹簧用AspectJ上annoted方法导致404错误

@Pointcut(value="execution(* com.company.app.features.*.controller.*.*(..))") 
public void controller() { } 

@Before("controller()") 
public void before(JoinPoint jp) { 
    // Log 
} 

即根据需要对在@Pointcut定义的包的所有方法能正常工作。

但是,当我尝试将@Before指向只注释为@GetMapping(..)的方法时,该URL会导致404错误,但是另一些方法通常会起作用。

我该怎么做?我尝试没有人正在努力:

  • 仅修订@Before:@Before("method() && @annotation(GetMapping)")
  • 修订只@Pointcut:@Pointcut(value="execution(@GetMapping com.company...
  • 修订只@Pointcut:@Pointcut(value="execution(@GetMapping * com.company...

同样的结果(错误404)是当我通过控制器类实现接口时,@Override@GetMapping注解的方法并将此会见作为第一段代码说的是从接口到@Pointcut。我建议背后有类似的东西。有人会解释我吗?

+0

before-method should not take an argument Joinpoint –

+0

感谢您的评论,但不解决我的问题。 –

+0

拦截控制器调用的惯用方法是使用[HandlerInterceptor](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/HandlerInterceptor.html)。 –

回答

1
@Pointcut(value="execution(* com.company.app.features.*.controller.*.*(..))") 
public void controller() { } 

@Pointcut(value="execution(@within(org.springframework......GetMapping)") 
public void getMapping() { } 


@Before("controller() && getMapping(object)") 
public void controllerGetMapping(Object objectIfYouNeedIt) { 
    // Log 
} 
+0

'getMapping()'方法的注释中有一个typpo。我已经提出了你的想法,最后我使用了@Pointcut(“@ annotation(org.springframework.web.bind.annotation.GetMapping)”),它运行良好,所以我即将接受你的答案。但是,你知道我的问题的第二部分的答案吗? :) –