I recently switched to two-phase injection, and this created an error in servlet binding. I am currently switching between two error modes and am not sure which direction is best taken.
The first error I encountered was:
com.sun.jersey.api.container.ContainerException: ResourceConfig instance does not contain root resource classes.
My servlet module looked like this:
public class MyServletModule extends JerseyServletModule { @Override protected void configureServlets() { bind(MyServlet.class).asEagerSingleton(); serve("/*").with(GuiceContainer.class); } }
I managed to remove this error by explicitly setting the com.sun.jersey.config.property.packages parameter.
public class MyServletModule extends JerseyServletModule { @Override protected void configureServlets() { bind(MyServlet.class).asEagerSingleton(); Map<String,String> parameters = new HashMap<String, String>(); parameters.put(PackagesResourceConfig.PROPERTY_PACKAGES, MyServlet.class.getPackage().getName()); serve("/*").with(GuiceContainer.class, parameters); } }
But when I do this, Guice tries to bind Just In Time, which does not respect @Inject in my servlet constructor.
com.google.inject.ConfigurationException: Guice configuration errors:
1) Unable to create a binding for MyServlet. It is already configured on one or more injectable injectors or private modules associated with MyServletModule.configureServlets (MyServletModule.java:44) If this was in PrivateModule, did you forget to open the link? while searching for MyServlet
1 error when com.google.inject.internal.InjectorImpl.getBinding (InjectorImpl.java:150)
My servlet has a @Inject constructor whose arguments cannot be bound just in time. After debugging in InjectorImpl, I believe that this is the reason why something fails when I use PROPERTY_PACKAGES.
I'm just not sure if PROPERTY_PACKAGES is being used correctly, and I need to fix some bindings? Or if this is the wrong direction, and I need to fix the original ResourceConfig error in another way.
Help or push in the right direction is welcome.