users@jersey.java.net

[Jersey] Re: jersey-guice: Missing dependency for constructor

From: Markus Karg <markus.karg_at_gmx.net>
Date: Fri, 14 Jan 2011 18:16:13 +0100

I had a similar message in my GlassFish log today after switching from
Jersey 0.x to Jersey 1.4 in one of our older applications. I solved it by
replacing the servlet parameter name by "javax.ws.rs.Application" (0.x used
a different name there). Possibly you have a typo in the name?

HTH
Markus

> -----Original Message-----
> From: Paul Sandoz [mailto:Paul.Sandoz_at_oracle.com]
> Sent: Freitag, 14. Januar 2011 11:01
> Cc: users_at_jersey.java.net
> Subject: [Jersey] Re: jersey-guice: Missing dependency for constructor
>
> Hi,
>
> I think Jersey is instantiating DoctorsResource instead of Guice.
>
> That would imply one of two things:
>
> 1) The web.xml is incorrect; or
>
> 2) There is a bug or inconsistency in the Guice integration related
> to scopes.
>
> I suspect it might be the latter. I notice you have not declared the
> scope of the DoctorsResource. Try annotating that class with the Guice
> annotation @RequestScoped or @Singleton. If that is the case can you
> please log an issue related to no-scope bindings.
>
> Paul.
>
> On Jan 14, 2011, at 6:27 AM, Gili wrote:
>
> >
> >
> > Gili wrote:
> >>
> >> Hi,
> >>
> >> I created a test program according to
> >> http://jersey.java.net/nonav/apidocs/latest/contribs/jersey-
> guice/com/sun/jersey/guice/spi/container/servlet/package-summary.html
> >> but for some reason whenever I try constructor injection I get:
> >>
> >> SEVERE: Missing dependency for constructor public
> >> foo.DoctorsResource(com.holdmyspot.search.server.Test) at parameter
> >> index
> >> 0
> >>
> >> Here is my config file (I verified this is being executed at load-
> >> time):
> >>
> >> public class GuiceConfig extends GuiceServletContextListener
> >> {
> >> @Override
> >> protected Injector getInjector()
> >> {
> >> return Guice.createInjector(new JerseyServletModule()
> >> {
> >> @Override
> >> protected void configureServlets()
> >> {
> >> bind(DoctorsResource.class);
> >> bind(Test.class).toInstance(new Test());
> >> serve("/*").with(GuiceContainer.class);
> >> }
> >> });
> >> }
> >> }
> >>
> >> And here is my resource:
> >>
> >> @Path("/doctors")
> >> public class DoctorsResource
> >> {
> >> private final Test test;
> >>
> >> @Inject
> >> public DoctorsResource(Test test)
> >> {
> >> this.test = test;
> >> }
> >> }
> >>
> >> I am fairly certain that Guice knows how to inject Test. Why then
> >> am I
> >> getting this error?
> >>
> >> Thanks,
> >> Gili
> >>
> >
> > Adding @InjectParam to the constructor argument fixed the problem.
> > That is:
> >
> > @Inject
> > public DoctorsResource(@InjectParam Test test)
> > {
> > this.test = test;
> > }
> >
> > Why does this work? And why isn't this mentioned by the
> aforementioned
> > Javadoc? :)
> >
> > Thanks,
> > Gili
> > --
> > View this message in context:
> http://jersey.576304.n2.nabble.com/jersey-guice-Missing-dependency-for-
> constructor-tp5920703p5920726.html
> > Sent from the Jersey mailing list archive at Nabble.com.