users@jersey.java.net

Re: [Jersey] Jersey-Guice is injecting nulls

From: Jonathan Leibiusky <ionathan_at_gmail.com>
Date: Mon, 8 Jun 2009 14:20:27 -0700 (PDT)

Worked perfectly! Thanks!
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?


On Fri, Jun 5, 2009 at 8:13 PM, Paul Sandoz (via Nabble) <
ml-user+51078-399195266_at_n2.nabble.com<ml-user%2B51078-399195266_at_n2.nabble.com>
> 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@...<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=3033322&i=0>
> >>> For additional commands, e-mail: users-help@...<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=3033322&i=1>
> >>>
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: users-unsubscribe@...<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=3033322&i=2>
> >> For additional commands, e-mail: users-help@...<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=3033322&i=3>
> >>
> >>
> >>
> >
> > --
> > 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@...<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=3033322&i=4>
> > For additional commands, e-mail: users-help@...<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=3033322&i=5>
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@...<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=3033322&i=6>
> For additional commands, e-mail: users-help@...<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=3033322&i=7>
>
>
>
> ------------------------------
> This email is a reply to your post @
> http://n2.nabble.com/Jersey-Guice-is-injecting-nulls-tp3031562p3033322.html
> You can reply by email or by visting the link above.
>
>

-- 
View this message in context: http://n2.nabble.com/Jersey-Guice-is-injecting-nulls-tp3031562p3045709.html
Sent from the Jersey mailing list archive at Nabble.com.