users@jersey.java.net

[Jersey] Re: jersey-guice inject subresources

From: Paul Sandoz <Paul.Sandoz_at_oracle.com>
Date: Thu, 9 Dec 2010 09:52:59 +0100

Hi Christopher,

There are a couple of errors in your code:

1) Jersey/JAX-RS does not currently support Guice/CDI//330 @Inject
semantics on methods that the JAX-RS framework
      invokes. Note that the @Inject on such methods will conflict
with method injection rules.
      See Devoxx presentation on what we could do about that.

2) The sub-resource locator, that returns a resource for further path
matching, should not be annotated with an @GET method.


Try this:

@RequestScoped
@Path("/items")
public class TopResource {
   // Don't directly inject the reference because we don't want to
instantiate at injection.
   @Inject Provider<SubResource> injectedSub;

   @Path("{itemId}")
   public SubResource getItem() {
      return injectedSub.get();
   }
}


If you want to depend on the Jersey API you can do this:

@RequestScoped
@Path("/items")
public class TopResource {

   @Path("{itemId}")
   public SubResource getItem(@InjectParam injectedSub) {
      return injectedSub;
   }
}

Hth,
Paul.

On Dec 8, 2010, at 7:02 PM, Christopher Piggott wrote:

> I've been reading old posts to the mailing list, trying to figure out
> if I'm doing this right. I think I am:
>
>
>
> import com.google.inject.Inject;
>
> @RequestScoped
> @Path("/items")
> public class TopResource {
> @GET
> @Path("{itemId}")
> @Inject
> public SubResource getItem(SubResource injectedSub)
> {
> return injectedSub;
> }
> }
>
>
>
> @RequestScoped
> public class SubResource {
> @PathParam("itemId") int itemId;
>
> @GET
> public String get()
> {
> return "Your item is number " + itemId;
> }
> }
>
>
>
> I think that should work. I can tell you for sure the following:
> * The TopResource is being created
> * An instance of SubResource is being created through injection
> * in the sub-resource, itemId is 0 ... so that's not working
>
> But the main problem I'm having is:
>
> WARN com.sun.jersey.spi.inject.Errors (173) The following warnings
> have been detected with resource and/or provider classes:
> WARNING: A HTTP GET method, public SubResource
> TopResource.get(SubResource), should not consume any entity.
>
>
> I read about a similar problem with "should not consume any entity"
> relating to MatrixParam but all I'm trying to do is pass down a path
> parameter. Shouldn't that be OK?
>
> --Chris