5

Guice AOPを使用して、Jerseyリソースの注釈付きメソッドをインターセプトすることは可能ですか?

依存性注入に関してJerseyと正常に構成されたGuice統合が問題なく機能していますが、構成されたインターセプターが注釈付きメソッドをまったくインターセプトしていません。

web.xml

<listener>
    <listener-class>my.package.GuiceConfig</listener-class>
</listener>
<filter>
    <filter-name>guiceFilter</filter-name>
    <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>guiceFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

GuiceConfig構成モジュール

public class GuiceConfig extends GuiceServletContextListener {

@Override
protected Injector getInjector() {
    return Guice.createInjector(new JerseyServletModule() {

            @Override
            protected void configureServlets() {

                bindInterceptor(Matchers.any(), 
                                Matchers.annotatedWith(RequiredAuthority.class), 
                                new AuthorisationInterceptor());

                Map<String, String> params = new HashMap<String, String>(); 
                params.put(JSP_TEMPLATES_BASE_PATH, "/WEB-INF/jsp"); 
                params.put(FEATURE_FILTER_FORWARD_ON_404, "true");
                params.put(PROPERTY_PACKAGES, "my.service.package");

                filter("/*").through(GuiceContainer.class, params);
            } 
        });
    }
}

RequiredAuthorityアノテーション

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RequiredAuthority {
    String value();
}

AuthorisationInterceptorの側面

public class AuthorisationInterceptor implements MethodInterceptor {

    public Object invoke(MethodInvocation methodInvocation) throws Throwable {

        // Allow invocation to process or throw an appropriate exception
    }
}

TempResourceJAX-RSリソースクラス

@Path("/temp")
public class TempResource {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @RequiredAuthority("PERMISSION")
    public String getTemp() {

        // Return resource normally
    }
}
4

1 に答える 1

5

configureServlets() が呼び出されていないようです:

bind(TempResource.class);
于 2012-06-11T21:26:06.640 に答える