1

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?

Dis Shishkov
  • 657
  • 7
  • 21
Ken Chen
  • 62
  • 1
  • 7

1 Answers1

0

If you want Guice to inject members in AbcResource you're going to need Guice to manage that class. I suspect you're going to need to add:

bind(AbcResource.class);

to your ConfigureServlets method. This won't interfere with any object management that Jersey performs.

condit
  • 10,852
  • 2
  • 41
  • 60
  • Thanks @condit. I have tried but seems it still doesn't work. I think I would need further investigate on it. – Ken Chen May 10 '13 at 03:20