與你正在嘗試做的問題是,你要像對待一個name binding annotation註釋。據我所知,這僅適用於過濾器和攔截器
我能想到的唯一方法是隻是爲了獲得從RequestEvent
的ExtendedUriInfo
在RequestEventListener
的onEvent
方法,然後做一些反射來獲取註釋。檢查它是否爲空。
例如
@Provider
public class TestApplicationListener implements ApplicationEventListener {
@Target({METHOD})
@Retention(RetentionPolicy.RUNTIME)
public static @interface SomeAnnotation {
}
@Override
public void onEvent(ApplicationEvent ae) {}
@Override
public RequestEventListener onRequest(RequestEvent re) {
return new RequestListener();
}
public static class RequestListener implements RequestEventListener {
@Override
public void onEvent(RequestEvent re) {
switch (re.getType()) {
case RESOURCE_METHOD_START:
if (hasAnnotation(re))
System.out.println("-- record method start event --");
break;
case RESOURCE_METHOD_FINISHED:
if (hasAnnotation(re))
System.out.println("-- record method end event --");
break;
}
}
private boolean hasAnnotation(RequestEvent e) {
ResourceMethod resourceMethod = e.getUriInfo().getMatchedResourceMethod();
Invocable invocable = resourceMethod.getInvocable();
SomeAnnotation annotation = invocable.getHandlingMethod()
.getAnnotation(SomeAnnotation.class);
return annotation != null;
}
}
}
請注意,您需要檢查RequestEvent.Type
,因爲ResourceMethod
將不會適用於所有事件類型。