吉斯+ Jersey:添加所有资源和提供程序,而不绑定到 Jersey Servlet

发布于 2024-11-29 08:19:44 字数 1590 浏览 0 评论 0原文

我目前正在使用 Jersey 1.8 结合 Guice 3.0 为我的 JPA 进行 DI 开发一个球衣应用程序。这很好地解决了我无法克服的一个主要问题:

我必须使用绑定手动将所有 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);

        }
    });
}

我希望能够省略对每个资源和提供程序使用绑定并在 jersey-guice 文档中发现一条评论:http://jersey.java.net/nonav/apidocs/1.8/contribs/jersey-guice/com/sun/jersey/guice/spi/container/servlet/package-summary.html

它基本上表明可以将球衣资源的注册传递给球衣 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);
             }
         }
     });

遗憾的是,我无法获得有关球衣资源注册过程的更多信息。

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

Im currently working on an jersey application using Jersey 1.8 in conjunction with Guice 3.0 for DI for my JPA. This works pretty well with one major issue which I wasn't able to overcome:

I have to manually add all Jersey Resource classes to the JerseyServletModule using bind:

@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);

        }
    });
}

I would like to be able to ommit the use of bind for every single resource and provider and found in the jersey-guice doc a remark: http://jersey.java.net/nonav/apidocs/1.8/contribs/jersey-guice/com/sun/jersey/guice/spi/container/servlet/package-summary.html

It basically states that one can pass the registration of jersey resources to the jersey servlet. However i was not able to manage this using said method:

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

Sadly I could not get more information about the registration process of jersey resources.

Any help would be appreciated. Of course I can provide additional info, if needed!
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美胚控场 2024-12-06 08:19:44

我回答自己的问题变得相当标准,但我从 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!

It becomes fairly standard that i answer my own question, but I found the answer from a Guice 2 Thread but fortunately it works with Guice 3 and 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");

There you go.

Dont forget to pass the params Map to your GuiceContainer.class!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文