2017-07-17 74 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)」),它運行良好,所以我即將接受你的答案。但是,你知道我的問題的第二部分的答案嗎? :) –