【问题标题】:How can I use Jersey's @QueryParam and Guice injection in a resource constructor?如何在资源构造函数中使用 Jersey 的 @QueryParam 和 Guice 注入?
【发布时间】:2016-07-22 16:10:36
【问题描述】:

我正在尝试在资源构造函数中同时使用 Jersey 的 @QueryParam 和 Guice 的 @Inject。从网上看,之前也有类似的问题问过我:
How can I mix Guice and Jersey injection?
http://users.jersey.dev.java.narkive.com/zlGMXuBe/can-queryparam-be-used-in-resource-constructor-along-with-guice-injection

这似乎是不可能的。但是,这些问题已经存在好几年了,那么我现在正在尝试做的事情可能吗?

以下是我正在尝试做的一些代码:

@Path("/mypath")
public class MyResource {
  private Manager manager;
  private String type;

  @Inject
  public MyResource(Manager manager,
                    @QueryParam("type") String type) {
    this.manager = manager;
    this.type = type;
  }

  @GET
  @Produces("text/plan")
  @Path("/{period}")
  public String myMethod(@PathParam("period") String period) {
    return manager.foo(period, type);
  }
}

谢谢!

【问题讨论】:

  • 这没有意义? MyResource 是一个单例并处理所有请求。在施工时没有请求,因此没有@QueryParam
  • @LanceJava 如果您删除 Guice @Inject 的东西,它可以工作。您可以在请求中传入查询参数,构造函数会将其设置为您传入的任何内容。
  • 好吧,我自己不是泽西岛用户。 Spring mvc 等使用单例而不是每个请求事件处理程序。我只能假设您需要以某种方式将 guice 插入 Jersey 注射器

标签: java jersey guice


【解决方案1】:

它对我有用。也许是与 Jersey 和 Guice 正确绑定有关的问题。

我使用您的资源定义和一些样板代码创建了一个最小的 Web 应用程序。

首先是应用初始化:

@WebListener
@Singleton
public class AppContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        new GuiceBootstrap().contextInitialized(sce);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        // no op
    }
}

你可以看到我在那里初始化了 Guice。这是 Guice 代码。

public class GuiceBootstrap extends GuiceServletContextListener {
    @Override
    protected Injector getInjector() {
        return Guice.createInjector((Module) binder -> binder.bind(Manager.class)
                                                             .to(ManagerImpl.class));
    }
}

它是 Java 8 语法,但如果您不使用 Java 8,它很容易转换为 pre-lambda 代码。我创建了一个只有一个绑定的 Guice 注入器。

Manager 和实现类很简单。

public interface Manager {
    String foo(String period, String type);
}

public class ManagerImpl implements Manager {
    @Override
    public String foo(String period, String type) {
        return "Got " + period + " " + type;
    }
}

最后是初始化 Jersey 并将其内部注入器 (HK2) 绑定到 Guice 的代码。

@ApplicationPath("api")
public class ApiRest extends ResourceConfig {

    @Inject
    public ApiRest(ServiceLocator serviceLocator, ServletContext servletContext) {
        packages("net.sargue.so38531044");
        GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);
        GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class);
        Injector injector = (Injector) servletContext.getAttribute(Injector.class.getName());
        if (injector == null)
            throw new RuntimeException("Guice Injector not found");
        guiceBridge.bridgeGuiceInjector(injector);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多