gpt4 book ai didi

java - Guice + Jersey : Add all resources and providers without bind to Jersey Servlet

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:55:59 25 4
gpt4 key购买 nike

我目前正在使用 Jersey 1.8 结合 Guice 3.0 为我的 JPA 开发一个 Jersey 应用程序。这对于我无法克服的一个主要问题非常有效:

我必须使用绑定(bind)手动将所有 Jersey 资源类添加到 JerseyServletModule:

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

@Override
protected void configureServlets() {

install(new JpaPersistModule("DBName"));
filter("/*").through(PersistFilter.class);

/* bind the REST resources and serve*/
bind(Hello.class);
serve("/*").with(GuiceContainer.class);

}
});
}

我希望能够省略对每个资源和提供者绑定(bind)的使用,并在 jersey-guice 文档中找到一个备注:http://jersey.java.net/nonav/apidocs/1.8/contribs/jersey-guice/com/sun/jersey/guice/spi/container/servlet/package-summary.html

它基本上说明了可以将 jersey 资源的注册传递给 jersey servlet。但是我无法使用上述方法来管理它:

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

@Override
protected void configureServlets() {
bind(GuiceResource.class);

Map<String, String> params = new HashMap<String, String>();
params.put(PackagesResourceConfig.PROPERTY_PACKAGES, "unbound");
serve("/*").with(GuiceContainer.class, params);
}
}
});

遗憾的是我无法获得更多关于 Jersey 资源注册过程的信息。

如有任何帮助,我们将不胜感激。当然,如果需要,我可以提供更多信息!谢谢。

最佳答案

我回答自己的问题变得相当标准,但我从 Guice 2 线程中找到了答案,但幸运的是它适用于 Guice 3 和 Jersey 1.8

/*
* The following line will scan ausbdsoccer.server.resources package for Jersey Resources
*/
params.put("com.sun.jersey.config.property.packages","ausbdsoccer.server.resources");

给你。

不要忘记将 params Map 传递给您的 GuiceContainer.class!

关于java - Guice + Jersey : Add all resources and providers without bind to Jersey Servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7026275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com