Guice:将ContainerRequestContext注入拦截器

时间:2020-11-03 03:44:05

标签: java guice dropwizard

我有一个Dropwizard应用程序,我希望能够将给定请求的ContainerRequestContext注入拦截器中,基本上是这样的:

# Guice configuration to set up the interceptor.  
Provider<ContainerRequestContext> ctxProvider = getProvider(ContainerRequestContext.class);
bindInterceptor(Matchers.any(), Matchers.any(), new SecurityInterceptor(ctxProvider));

我的拦截器将如下所示:

public class SecurityInterceptor implements MethodInterceptor {

    private final Provider<ContainerRequestContext> ctxProvider;

    public SecurityInterceptor(Provider<ContainerRequestContext> ctxProvider) {

        this.ctxProvider = ctxProvider;
    }

    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        ContainerRequestContext containerRequestContext = ctxProvider.get();
        // Logic with using ctx
        return methodInvocation.proceed();
    }
}

但是,当我尝试以上操作时,启动时出现以下错误

1) No implementation for javax.ws.rs.container.ContainerRequestContext was bound.

如何配置Guice,以便可以注入ContainerRequestContext提供程序?

0 个答案:

没有答案
相关问题