users@jersey.java.net

Re: [Jersey] Jersey-Guice is injecting nulls

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 09 Jun 2009 11:45:33 +0200

On Jun 8, 2009, at 11:20 PM, Jonathan Leibiusky wrote:

> Worked perfectly! Thanks!

Great.

>
> Now I would like not to bind my resource, it would be nice if that's
> it's automated somehow.
> Is it possible? I mean, Jersey check resources in a specific package
> for example, could I achieve the same with jersey-guice?

Marc (Hadley) suggested exactly that to me the other day :-)

We need to utilize a package scanner as part of the guice support. It
seems like it is appropriate to provide an extension of ServletModule
that easily builds according to such cases.

Could you log an issue?

Thanks,
Paul.



>
>
>
> On Fri, Jun 5, 2009 at 8:13 PM, Paul Sandoz (via Nabble) <ml-user
> %2B51078-399195266@...> wrote:
>
> On Jun 5, 2009, at 2:36 PM, Jonathan Leibiusky wrote:
>
> >
> > Mmm... sorry but I don't seems to understand what I am missing here.
> > In my web.xml I declare Jersey Servlet
> > The only thing that I saw that I do different in the URL that you
> > sent is
> > that in TroyGuiceConfig I should add:
> > serve("/*").with(GuiceContainer.class);
> >
>
> Yes, and *do not* declare the Jersey servlet in the web.xml, so your
> web.xml can be:
>
> <web-app>
> <listener>
> <listener-class>com.olx.troy.TroyGuiceConfig</listener-class>
> </listener>
> <filter>
> <servlet-name>Guice Filter</servlet-name>
> <servlet-class>com.google.inject.servlet.GuiceFilter</servlet-
> class>
> </filter>
> <filter-mapping>
> <filter-name>Guice Filter</filter-name>
> <url-pattern>/*</url-pattern>
> </filter-mapping>
> </web-app>
>
> Paul.
>
>
> > I did that but now I'm getting this error:
> > com.sun.jersey.api.container.ContainerException: The ResourceConfig
> > instance
> > does not contain any root resource classes.
> >
> > It seems like by doing that I'm overriding the behaviour set in the
> > web.xml
> > for the jersey servlet.
> >
>
> >
> >
> >
> > Paul Sandoz wrote:
> >>
> >>
> >> On Jun 5, 2009, at 9:45 AM, Jonathan Leibiusky wrote:
> >>
> >>>
> >>> Hi!
> >>> Great framework and really nice guice integration!
> >>
> >> Thanks!
> >>
> >>
> >>> I'm having problems with
> >>> guice injection, it is injecting null. Actually I'm not 100% sure
> >>> that the
> >>> resource is being instantiated by Guice.
> >>> I'm using jersey 1.1.1-ea-SNAPSHOT.
> >>
> >>
> >> You do not declare the Jersey servlet in the web.xml, instead see
> >> here:
> >>
> >> https://jersey.dev.java.net/nonav/apidocs/1.1.0-ea/contribs/jersey-guice/com/sun/jersey/guice/spi/container/servlet/package-summary.html
> >>
>
> >> Basically you declare the Jersey Guice servlet in the
> ServletModel of
> >> your TroyGuiceConfig.
> >>
> >> Paul.
> >>
> >>>
> >>> In my web.config I have:
> >>> <servlet>
> >>> <servlet-name>Troy</servlet-name>
> >>>
> >>> <servlet-
> >>> class>com.sun.jersey.spi.container.servlet.ServletContainer</
> >>> servlet-
> >>> class>
> >>> <init-param>
> >>> <param-name>com.sun.jersey.config.property.packages</param-name>
> >>> <param-value>com.olx.troy.resources</param-value>
> >>> </init-param>
> >>> <load-on-startup>1</load-on-startup>
> >>> </servlet>
> >>> <servlet-mapping>
> >>> <servlet-name>Troy</servlet-name>
> >>> <url-pattern>/*</url-pattern>
> >>> </servlet-mapping>
> >>> <listener>
> >>> <listener-class>com.olx.troy.TroyGuiceConfig</listener-class>
> >>> </listener>
> >>> <filter>
> >>> <filter-name>Guice Filter</filter-name>
> >>> <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
> >>> </filter>
> >>>
> >>> The com.olx.troy.TroyGuiceConfig is:
> >>> public class TroyGuiceConfig extends GuiceServletContextListener {
> >>>
> >>> @Override
> >>> protected Injector getInjector() {
> >>> return Guice.createInjector(new ServletModule() {
> >>> @Override
> >>> protected void configureServlets() {
> >>> bind(Something.class).to(SomethingImpl.class);
> >>> }
> >>> });
> >>> }
> >>>
> >>> }
> >>>
> >>> And my com.olx.troy.resources.CategoryResource is:
> >>> @Path("/category")
> >>> @Produces("application/json")
> >>> public class CategoryResource {
> >>> private Something something;
> >>>
> >>> @Inject
> >>> public CategoryResource(Something something) {
> >>> super();
> >>> this.something = something;
> >>> }
> >>>
> >>> @GET
> >>> public List<Category> listAll(@QueryParam(value = "country") int
> >>> countryId) {
> >>> List<Category> list = new ArrayList<Category>();
> >>> System.out.println(something.lala());
> >>> list.add(new Category(185, "For Sale"));
> >>> return list;
> >>> }
> >>> }
> >>>
> >>>
> >>> As you can see I want Guice to inject an instance of
> >>> SomethingImpl.class to
> >>> CategoryResource constructor. But instead I'm getting a null.
> >>>
> >>> I believe I'm missing something.
> >>>
> >>> Thanks!
> >>>
> >>>
> >>> Jonathan
> >>>
> >>> --
> >>> View this message in context:
> >>> http://n2.nabble.com/Jersey-Guice-is-injecting-nulls-tp3031562p3031562.html
> >>> Sent from the Jersey mailing list archive at Nabble.com.
> >>>
> >>>
> >>>
> ---------------------------------------------------------------------
> >>> To unsubscribe, e-mail: users-unsubscribe@...
> >>> For additional commands, e-mail: users-help@...
> >>>
> >>
> >>
> >>
> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: users-unsubscribe@...
> >> For additional commands, e-mail: users-help@...
> >>
> >>
> >>
> >
> > --
> > View this message in context: http://n2.nabble.com/Jersey-Guice-is-injecting-nulls-tp3031562p3032956.html
>
> > Sent from the Jersey mailing list archive at Nabble.com.
> >
> >
> >
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@...
> > For additional commands, e-mail: users-help@...
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@...
> For additional commands, e-mail: users-help@...
>
>
>
> View this message in context: Re: [Jersey] Jersey-Guice is injecting
> nulls
> Sent from the Jersey mailing list archive at Nabble.com.