【问题标题】:Guice provider for UriInfo using RestEasy使用 RestEasy 的 UriInfo 的 Guice 提供程序
【发布时间】:2019-07-16 17:12:12
【问题描述】:

我有一个网络应用程序正在从泽西岛移植到 RestEasy。该应用程序将 Guice 用于 CDI。

该应用程序使用 Guice 提供程序来注入 UriInfo。在 Jersey 版本中,此代码如下所示

public static class JerseyIntegrationModule extends AbstractModule {
    @Override protected void configure() {
        bind(WebApplication.class).to(WebApplicationImpl.class).in(Scopes.SINGLETON);
    }

    @Provides @RequestScoped
    public HttpContext getHttpContext(WebApplication webapp) {
      return webapp.getThreadLocalHttpContext();
    }

    @Provides @RequestScoped
    public UriInfo getUriInfo(HttpContext httpContext) {
      return httpContext.getUriInfo();
    }
}

所有这些WebApplicationHttpContext 等类都是针对泽西岛的。问题是如何在 RestEasy 下提供类似的东西。

一个尝试是这样的

public class MyServlet extends ServletModule {
    @Provides @RequestScoped
    public UriInfo getUriInfo(@Context UriInfo info) {
        return info;
    }
}

但这会导致 Guice 的注入代码中的堆栈溢出。

我知道@Context 属性应该让我在 RestEasy 下注入 UriInfo,但我不知道在 Guice 提供程序中使用它。

该应用将部署在 Wildfly 15 上。

非常感谢任何帮助,因为这让我头晕目眩。

【问题讨论】:

    标签: java dependency-injection jersey guice resteasy


    【解决方案1】:

    似乎有一个单一的解决方案

    public class MyServlets extends ServletModule {
    
      @Provides @RequestScoped
      public UriInfo getUriInfo() {
        return ResteasyProviderFactory.getContextData(UriInfo.class);
      }
    
    }
    

    【讨论】:

      【解决方案2】:

      Resteasy 附带提供 UriInfo、HttpServletRequest 等的 RequestScopeModule

      更多信息http://docs.jboss.org/resteasy/docs/3.6.3.Final/userguide/html_single/index.html#Guice1

      【讨论】:

      • 这显然是最好的方法,但有些事情让我感到困惑。从您链接的文档中“请注意,如果您的任何模块扩展了 com.google.inject.servlet.ServletModule,则已经添加了 RequestScopeModule。”我的一个模块确实扩展了 ServletModule,但我没有获得 UriInfo 的绑定。有什么想法吗?
      猜你喜欢
      • 2016-08-02
      • 1970-01-01
      • 1970-01-01
      • 2016-07-26
      • 1970-01-01
      • 1970-01-01
      • 2018-05-30
      • 1970-01-01
      • 2012-08-03
      相关资源
      最近更新 更多