users@jersey.java.net

Re: [Jersey] Required parameters on a Resource

From: DirkM <dirk_at_olx.com>
Date: Fri, 14 Aug 2009 15:47:36 -0500 (CDT)

I had a go at writing the class, and although in the logs the Provider
appears to get picked up, the getStringReader method is never actually
called. I'm no expert in generics so perhaps I've missed something obvious.
Here's my code:

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

    @Override
    public StringReader getStringReader(Class type, Type genericType,
Annotation[] annotations) {

        // Create a copy of the annotations array, without the @Required
        // annotation
        Annotation[] otherAnnotations = new Annotation[annotations.length];
        int i = 0;
        for (Annotation annotation : annotations) {
            if (!annotation.getClass().equals(Required.class)) {
                otherAnnotations[i++] = annotation;
            }
        }

        // If all the annotations were copied, that means none of them was
the
        // @Required annotation.
        if (i == annotations.length) {
            return null;
        }

        final StringReader<?> sr = srw.getStringReader(type, genericType,
otherAnnotations);
        if (sr == null) {
            return null;
        }

        return new StringReader() {
            public Object fromString(String value) {
                if (value == null) {
                    throw new WebApplicationException(400);
                }
                return sr.fromString(value);
            }
        };
    }
}
-- 
View this message in context: http://n2.nabble.com/Required-parameters-on-a-Resource-tp3446928p3447394.html
Sent from the Jersey mailing list archive at Nabble.com.