users@jersey.java.net

Re: [Jersey] Jersey-Guice is injecting nulls

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 05 Jun 2009 12:21:57 -0700

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_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>