Note that there are some explanatory texts on larger screens.

plurals
  1. POJersey-Guice doesn't process bound resources if injector is a child?
    text
    copied!<p>I'm using Jersey-Guice to configure a Jersey app, following this <a href="http://randomizedsort.blogspot.com/2011/05/using-guice-ified-jersey-in-embedded.html" rel="nofollow">template</a>. Everything works fine if the <code>Injector</code> returned by the <code>GuiceServletContextListener.getInjector()</code> method is created by <code>Guice.createInjector()</code>. If that injector is instead the child of another injector, then the bound resources (e.g., <code>MyResource</code> in the code below) are never added to the Jersey <code>ResourceConfig</code> and Jersey crashes with a complaint about missing root resources. I don't think the bound resources are even scanned, because the usual "INFO: Registering my.example.MyResource as a root resource class" doesn't appear in the log.</p> <p>Any ideas why this might be happening? Both versions are show below.</p> <p>As an additional question: I'm trying to use the child injector because I want to configure my application data service object in my Main() class. More than just the Jersey resources need access to it. I still need it injected into the Jersey resources. </p> <p>If there's a better way to share the application singleton between the application Injector and servlet injector (better than my current approach of the servlet injector being a child of the application injector), please let me know.</p> <p>This version works.</p> <pre><code>public class MyConfig extends GuiceServletContextListener { @Override protected Injector getInjector() { return Guice.createInjector(new ServletModule() { @Override protected void configureServlets() { bind(MyResource.class); serve("*").with(GuiceContainer.class); } }); } } </code></pre> <p>The following code does not work though.</p> <pre><code> public class MyConfig extends GuiceServletContextListener { final Injector parentInjector; public MyConfig(Injector injector) { this.parentInjector = injector; } @Override protected Injector getInjector() { return parentInjector.getChildInjector(new ServletModule() { @Override protected void configureServlets() { bind(MyResource.class); serve("*").with(GuiceContainer.class); } }); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload