Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

jersey

guice

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.

like image 307
Johnnycube Avatar asked Aug 11 '11 12:08

Johnnycube


1 Answers

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!

like image 127
Johnnycube Avatar answered Oct 03 '22 05:10

Johnnycube