在 Jersey 中使用 Guice AOP 拦截方法 [英] Method interception in Jersey using Guice AOP

查看:40
本文介绍了在 Jersey 中使用 Guice AOP 拦截方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 Guice AOP 拦截 Jersey 资源上的带注释的方法?

Is it possible to use Guice AOP to intercept an annotated method on a Jersey resource?

我在依赖注入方面成功配置了 Guice 与 Jersey 的集成,没有任何问题,但是我配置的拦截器根本没有拦截我的注释方法.

I have a successfully configured Guice integration working with Jersey with respect to Dependency Injection without any problems, however my configured Interceptor is not intercepting my annotated method at all.

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();
}

授权拦截器方面

public class AuthorisationInterceptor implements MethodInterceptor {

    public Object invoke(MethodInvocation methodInvocation) throws Throwable {

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

TempResource JAX-RS 资源类

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

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

        // Return resource normally
    }
}

推荐答案

看起来 configureServlets() 没有调用:

Looks like configureServlets() isn't calling:

bind(TempResource.class);

这篇关于在 Jersey 中使用 Guice AOP 拦截方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆