users@jersey.java.net

[Jersey] Re: Pattern for sub-resources with injection and arguments

From: Gili <cowwoc_at_bbs.darktech.org>
Date: Tue, 29 Mar 2011 12:27:18 -0700 (PDT)

Paul Sandoz wrote:
>
> Hi,
>
> Guice 2.0 has a nice pattern for the case when obtain an instance from
> a DI framework where additional non-injected parameters need to be
> passed:
>
> http://code.google.com/docreader/#p=google-guice&amp;s=google-
> guice&amp;t=AssistedInject
>
> I know a number of developers have been scratching their heads other
> this one (including me).
>
> If you use Guice to manage your resources then you can use
> AssistedInject, which i presume should work with Jersey as i have not
> tested it.
>
> Otherwise you can follow a factory pattern:
>
> public class MyResourceFactory {
>
> private final UriInfo ui;
>
> public class MyResourceFactory(@Context UriInfo ui) {
> this.ui = ui;
> }
>
> public MyResource create(String param) {
> return new MyResource(param);
> }
>
> public class MyResource {
> private final String param;
>
> public class MyResource(String param) {
> this.param = param;
> }
> ...
> }
> }
>
>
> @Path(&quot;{id}&quot;)
> public MyResource get(@Context ResourceContext rc,
> @PathParam(&quot;id&quot;)
> String id) {
> return rc.get(MyResourceFactory.class).create(id);
> }
>
> Paul.
>

Hi Paul,

I'm trying to do exactly what you mentioned: using guice-assistedinject with
sub-resources that refer to UriInfo. Guice seems to inject UriInfo into
top-level resources just fine but fails for sub-resources with the following
error:

   No implementation for javax.ws.rs.core.UriInfo was bound.

Have you ever gotten this working?

Thanks,
Gili

--
View this message in context: http://jersey.576304.n2.nabble.com/Pattern-for-sub-resources-with-injection-and-arguments-tp3135527p6220554.html
Sent from the Jersey mailing list archive at Nabble.com.