users@jersey.java.net

Re: [Jersey] Jersey-Guice is injecting nulls

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

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