users@jersey.java.net

Re: [Jersey] Required parameters on a Resource

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 14 Aug 2009 21:25:09 +0200

Hi,

It should be possible to support your own @Required annotation by
writing a special StringReaderProvider:

  https://jersey.dev.java.net/nonav/apidocs/1.1.1-ea/jersey/com/sun/jersey/spi/StringReaderProvider.html

Off the top of my head here is some psuedo code:

  @Provider
  public SerializableStringProvider implements StringReaderProvider {
    @Context StringReaderWorkers srw;

    public StringReader getStringReader(Class<?> type, Type
genericType, Annotation[] annotations) {
      if (/annotations does not contain @Required/)
        return null;

      annotations = /remove required from annotations/

      final StringReader sr = srw.getStringReaderProvider(type,
genericType, annotatons);
      if (sr == null) return null;

      return new StringReader() {
        public Object fromString(String value) {
          if (value == null)
            throw new WebApplicationException(400);
          return sr.fromString(value);
        }
      }
    }

The above, suitably converted to Java, should work, if it does not
then it is a bug we need to fox.

Paul.

On Aug 14, 2009, at 9:04 PM, DirkM wrote:

>
> I'd like to indicate that a query parameter is required. There
> doesn't seem
> to be a way to do this using the @QueryParam annotation. Is there a
> @Required annotation or something similar that I'm missing?
> The behaviour that I would expect is that it would return a 400
> response if
> the parameter is missing.
>
> I know of two workarounds:
> 1. use classes (eg Integer instead of int) and check to see if it's
> null
> I don't really want to do this in every method where I have a
> required field
>
> 2. Write a custom parameter class
> I don't want to do that for every field type (RequiredInteger,
> RequiredString...)
>
> Please let me know if there's a way of requiring a parameter in
> Jersey.
> Thanks,
> Dirk
> --
> View this message in context: http://n2.nabble.com/Required-parameters-on-a-Resource-tp3446928p3446928.html
> Sent from the Jersey mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>