users@jersey.java.net

Can _at_QueryParam be used in resource constructor along with Guice injection?

From: ycx4321 <ycx4321_at_gmail.com>
Date: Sat, 30 Jan 2010 12:16:30 -0800

Hello,

Is it possible to use @QueryParam in the resource constructor? In other
words, I found that I couldn't do this with Jersey integration of Guice:

@Path("/mypath")
@RequestScoped
public class MyResource {
   private final GuiceService myGuiceService;
   private final String query;

   @Inject
   public MyResource(@MyGuiceService GuiceService guiceServiceImpl,
                                     @QueryParam("q") String query)
   {
      this.myGuiceService = guiceServiceImpl;
      this.query = query;
   }

   @Get
   @Produces("text/plan")
   public String hello() {
      return "hello";
   }
}

When I ran it in Jersey, I got this error from Guice:

com.google.inject.internal.ComputationException:
com.google.inject.internal.ComputationException:
java.lang.ClassCastException: $java.lang.String$$FastClassByGuice$$473e3665
cannot be cast to com.google.inject.internal.cglib.reflect.FastClass

But the program would run fine if I change the resource definition to:

/** Shortened class definition for illustration */
public class MyResource {
    @QueryParam("q") String query;

    @Inject
    public MyResource(@MyGuiceService GuiceService guiceServiceImpl) {
         this.myGuiceService = guiceServiceImpl;
    }
}

So why can't I put @QueryParam in the parameter list of the constructor? I
prefer the constructor injection to the field injection. I read about the
AssistedInjection in Guice but how do I use it in Jersey?

Thanks,
yc