I have referred to this link for the guide on how to do it. https://jersey.java.net/nonav/apidocs/latest/jersey/contribs/jersey-guice/com/sun/jersey/guice/spi/container/servlet/package-summary.html
I have followed what is shown there.
In web.xml:
<listener>
<listener-class>com.oocl.ir4.doc.common.servlet.GuiceServletConfig</listener-class>
</listener>
<filter>
<filter-name>GuiceFilter</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>GuiceFilter</filter-name>
<url-pattern>/rest/*</url-pattern>
</filter-mapping>
My guice config:
public class GuiceServletConfig extends GuiceServletContextListener {
@Override
protected Injector getInjector() {
return Guice.createInjector(new JerseyServletModule() {
@Override
protected void configureServlets() {
Map<String, String> params = new HashMap<String, String>();
params.put(PackagesResourceConfig.PROPERTY_PACKAGES, "xxx.yyy");
serve("/rest/*").with(GuiceContainer.class, params);
}
});
}
}
Below Resource class actually is managed by Jersey already which I don't want to be managed by Guice. Its package is scanned by Jersey but unbound in Guice Config:
package xxx.zzz;
@Path("/xxx/zzz")
public class AbcResource extends Resource {
@Inject
private AbcServiceImpl abcService;
...
}
The class I want Guice to inject to resource:
package xxx.yyy;
@Singleton
public class AbcServiceImpl {
...
}
However, it's not working. NullPointerException
occurs when I access the abcServiceImpl in the AbcResource.
Anyone have any hint on why or how to check what are maintained by Guice container so that I can check on it?