|
From: Brian M. <bri...@gm...> - 2014-05-19 13:04:28
|
Thank you. They’re all Booleans now. I felt I needed to report it in case it wasn’t desired behavior.
On May 19, 2014, at 2:16 AM, Weinan Li <we...@re...> wrote:
> Hi Brian,
>
> After some investigations I could confirm RESTEasy will use StringToPrimitive to deal with primitive types:
>
> package org.jboss.resteasy.util;
>
> /**
> * @author <a href="mailto:bi...@bu...">Bill Burke</a>
> * @version $Revision: 1 $
> */
> public class StringToPrimitive {
> public static Object stringToPrimitiveBoxType(Class primitiveType, String value) {
> if (primitiveType.equals(String.class)) return value;
> if (primitiveType.equals(boolean.class)) {
> if (value == null) return Boolean.FALSE;
> return Boolean.valueOf(value);
> }
> if (value == null) value = "0";
> if (primitiveType.equals(int.class)) return Integer.valueOf(value);
> if (primitiveType.equals(long.class)) return Long.valueOf(value);
> if (primitiveType.equals(double.class)) return Double.valueOf(value);
> if (primitiveType.equals(float.class)) return Float.valueOf(value);
> if (primitiveType.equals(byte.class)) return Byte.valueOf(value);
> if (primitiveType.equals(short.class)) return Short.valueOf(value);
> if (primitiveType.equals(boolean.class)) return Boolean.valueOf(value);
> return null;
>
> }
> }
>
> So obviously Boolean.valueOf(“1”) will return false. And seems we can’t bypass this in StringParameterInjector:
>
> try
> {
> if (baseType.isPrimitive()) return StringToPrimitive.stringToPrimitiveBoxType(baseType, strVal);
> }
>
> I’d suggest you to use Boolean type as your query parameter, which works correctly.
>
>
>
> Weinan Li / JBoss
>
>> On May 15, 2014, at 4:22 PM, Weinan Li <we...@re...> wrote:
>>
>> Hi Brian,
>>
>> I’ll look into your example.
>>
>>
>> Weinan Li / JBoss
>>
>>> On May 15, 2014, at 5:34 AM, Brian Mauter <bri...@gm...> wrote:
>>>
>>> Hi, I’d like to accept values other than “true” or “false” for boolean QueryParams (for example, “0” or “1”). It seems that my ParamConverter isn’t being invoked for primitives. I’ve attached a minimal example of what I’m trying to do but some of the most relevant details can be found below.
>>>
>>> My TestService method:
>>> @GET
>>> @Produces(MediaType.TEXT_PLAIN)
>>> public String test( @QueryParam("a") boolean a, @QueryParam("b") Boolean b, @QueryParam("c") int c,
>>> @QueryParam("d") Integer d, @QueryParam("e") String e, @QueryParam("f") Date f ) {
>>> return MessageFormat.format( "a={0}, b={1}, c={2}, d={3}, e={4}, f={5}", a, b, c, d, e, f );
>>> }
>>>
>>> I invoke it with:
>>> curl -4 -v http://localhost:8080/rt/test?a=1\&b=1\&c=0\&d=0\&e=e\&f=20140514160412-0500
>>>
>>> I get back:
>>> a=false, b=true, c=0, d=0, e=e, f=5/14/14 4:04 PM
>>>
>>> My two ParamConverters log their arguments in the getConverter, toString and fromString methods. Here’s the log:
>>> 2014-05-14 16:22:31,656 http-bio-8080-exec-1 DEBUG [BooleanParamConverter] rawType=class java.lang.Boolean, genericType=class java.lang.Boolean, annotations=[Ljava.lang.annotation.Annotation;@5ae29e22
>>> 2014-05-14 16:22:31,657 http-bio-8080-exec-1 DEBUG [BooleanParamConverter] rawType=class java.lang.Integer, genericType=class java.lang.Integer, annotations=[Ljava.lang.annotation.Annotation;@7f42ef92
>>> 2014-05-14 16:22:31,657 http-bio-8080-exec-1 DEBUG [DateParamConverter] rawType=class java.lang.Integer, genericType=class java.lang.Integer, annotations=[Ljava.lang.annotation.Annotation;@7f42ef92
>>> 2014-05-14 16:22:31,657 http-bio-8080-exec-1 DEBUG [BooleanParamConverter] rawType=class java.lang.String, genericType=class java.lang.String, annotations=[Ljava.lang.annotation.Annotation;@49cd8090
>>> 2014-05-14 16:22:31,657 http-bio-8080-exec-1 DEBUG [DateParamConverter] rawType=class java.lang.String, genericType=class java.lang.String, annotations=[Ljava.lang.annotation.Annotation;@49cd8090
>>> 2014-05-14 16:22:31,657 http-bio-8080-exec-1 DEBUG [BooleanParamConverter] rawType=class java.util.Date, genericType=class java.util.Date, annotations=[Ljava.lang.annotation.Annotation;@6ff09a85
>>> 2014-05-14 16:22:31,657 http-bio-8080-exec-1 DEBUG [DateParamConverter] rawType=class java.util.Date, genericType=class java.util.Date, annotations=[Ljava.lang.annotation.Annotation;@6ff09a85
>>> 2014-05-14 16:22:31,684 http-bio-8080-exec-1 DEBUG [BooleanParamConverter] fromString value=1
>>> 2014-05-14 16:22:31,684 http-bio-8080-exec-1 DEBUG [DateParamConverter] fromString value=20140514160412-0500
>>>
>>> Notice that parameter a in my TestService is a boolean and b is a Boolean. a is never sent to the ParamConverter whereas b is. The same goes for parameters c and d. Is there a rule that says primitives will always use a built-in ParamConverter?
>>>
>>> This is Tomcat 7.0.39 and RestEasy 3.0.7.Final.
>>>
>>> Thanks,
>>> -Brian
>>>
>>> ------------------------------------------------------------------------------
>>> "Accelerate Dev Cycles with Automated Cross-Browser Testing - For FREE
>>> Instantly run your Selenium tests across 300+ browser/OS combos.
>>> Get unparalleled scalability from the best Selenium testing platform available
>>> Simple to use. Nothing to install. Get started now for free."
>>> http://p.sf.net/sfu/SauceLabs
>>>
>>> _______________________________________________
>>> Resteasy-developers mailing list
>>> Res...@li...
>>> https://lists.sourceforge.net/lists/listinfo/resteasy-developers
>>>
|