users@jersey.java.net

Re: [Jersey] Can _at_QueryParam be used in resource constructor along with Guice injection?

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 02 Feb 2010 11:27:01 +0100

Hi,

Unfortunately not. I prefer constructor injection too.

This is a limitation in Guice that i discuss here:

   http://blogs.sun.com/sandoz/entry/guice_2_0_is_almost

I never got around to counter-pointing the details in Bob's comment.

 From a JAX-RS ease-of-use perspective it is unacceptable for
developers to have to bind query parameters, or any other parameters
for that matter, as constants in the Injector.

Further more, the JAX-RS annotations are not meta-annotated.

I would like to see Guice allow for frameworks to hook in to support
stuff additional requirements (where as restricting the end user so
that it would make it hard for them to do the same thing). Thus i
would like to define JAXRSJerseyModel that extends Guice wth Jersey/
JAX-RS requirements for the declared bindings.

As i said it is almost there. I just do not currently have the time to
hack on GuiceyFruit to enable such functionality :-(

Paul.


On Jan 30, 2010, at 9:16 PM, ycx4321 wrote:

> 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
>