users@jersey.java.net

Re: [Jersey] Null parameters to resource methods?

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 26 Oct 2009 09:49:13 +0100

HI Chris,

@GET requests cannot consume a request entity. We restrict things to
stop the bad practice of using GET incorrectly.

There are two solutions:

1) defer to another method; or

2) add an InjectableProvider that supports injection of a null value
for @NullParam annotated types:

@Provider
public class NullInjectableProvider implements
InjectableProvider<NullParam, Type> {
      public ComponentScope getScope() {
          return ComponentScope.Singleton;
      }

      public Injectable getInjectable(ComponentContext ic, NullParam
a, Type c) {
          return new Injectable() {
              public Object getValue() {
                  return null;
              }
          }
      }
}

Paul.

On Oct 24, 2009, at 9:42 PM, Chris Hubick wrote:

> Hi.
>
> I want my resource class to have a method like:
>
> @GET
> public MyResource getResource(CustomResourceSpec optional)
>
> Where when Jersey matches a request to that method, it would call it
> with a null CustomResourceSpec argument, but where other classes can
> reuse the same method, supplying a non-null argument.
>
> Is there any facility like this?
>
> As shown, I get an error like "A HTTP GET method, getResource, should
> not consume any entity."
>
> I tried annotating the arg with a fake @QueryParam("xxx"), which
> should
> never really have a value, but I get an error like "Method,
> getResource,
> annotated with GET ... is not recognized as valid Java method
> annotated
> with @HttpMethod", which I'm pretty sure is because it doesn't know
> about CustomResourceSpec, as it will send null for a primary type
> argument.
>
> Am I stuck creating extra Jersey/JAX specific no-arg wrapper methods
> in
> all these cases?
>
> I wish there was a @NullParam annotation or something to tell Jersey
> to
> just supply null for those parameters.
>
> Advice appreciated, Thanks!
>
> --
> Chris Hubick
> mailto:chris_at_hubick.com
> http://www.hubick.com/chris/
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>