You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Robert Roland <rr...@shopzilla.com> on 2010/03/08 20:47:58 UTC

ParameterHandler for a Long array

Hi all,

I'm attempting to make my service accept a URL like:

/getEffectiveBid?offerIds=1;2;3;4

I know that I can use a Long[] for a URL like:

/getEffectiveBid?offerIds=1&offerIds=2&offerIds=3

However - this URL format is not acceptable.  I want to support a semicolon
delimited list.

I've tried registering a ParameterHandler:

    <jaxrs:providers>
      <bean
class="com.shopzilla.ms.bidding.service.util.LongArrayParameterHandler"/>
    </jaxrs:providers>

My method looks like this:

    @Override
    @Path("getEffectiveBid")
    @Produces( { MediaType.APPLICATION_XML, "application/fastinfoset",
MediaType.APPLICATION_JSON })
    @GET
    public GetEffectiveBidResponse getEffectiveBid(@QueryParam("offerIds")
Long[] offerIds) throws Exception {

My ParameterHandler is never called, and I get the following error:

Mar 8, 2010 11:45:29 AM org.apache.cxf.jaxrs.utils.InjectionUtils
handleParameter
SEVERE: Class java.lang.Long can not be instantiated using a constructor
with a single String argument

How do I do this?  I've tried looking at the mailing lists, but it seems
everyone gives up and uses a String parameter, which just seems... dirty. :)

Thanks much for your help,

Rob
-- 
View this message in context: http://old.nabble.com/ParameterHandler-for-a-Long-array-tp27826543p27826543.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: ParameterHandler for a Long array

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi Robert

Apologies it took me so long to reply :-), only today I had a chance to look
into this issue.
What happens is that it is JAXRS the spec which is partially 'interfering'.
Basically, when one says

@QueryParam("id") List<Long> ids

what it means is that queries like
?id=1&id=2&id=3

are expected to be deal with with the list (in this example) containing 1,
2, 3 (and similarly for Set). I've also added a support for arrays like
Long[].

Thus, when the runtime sees

@QueryParam("id") List<Long> ids
or
@QueryParam("id") Long[] ids

it is parsing a query string into a usual name/value pairs and then tries to
convert each *individual* value of matching pairs (with the name equal to
'id' in this example) into Long (in this example), thus
ParameterHandler<Long[]> will not be checked.

It is only the case for collections/arrays where ParameterHandlers are not
visible.

So given the above and that you'd like this format be accepted :

>> /getEffectiveBid?offerIds=1;2;3;4

then what you might want to do, instead of registering a custom
ParameterHandler, is to register a custom RequestHandler input filter which
will overwrite a Message.QUERY_STRING Message property :

String query = (String)message.get(Message.QUERY_STRING);
if (query != null) {
    String[] pair = query.split("=");
    if (pair[0].equals("offerIds")) {
        String[] ids = pair[1].split(";");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < ids.length; i++) {
            sb.append(pair[0]).append("=").append(ids[i]);
            if (i + 1 < ids.length) {
                sb.append("&");
            }
        }
        message.put(Message.QUERY_STRING, sb.toString());
    }
}

this will let you to have signatures like

@QueryParam("id") Long[] ids

and queries like

/getEffectiveBid?offerIds=1;2;3;4

at a fairly low cost

hope it helps, Sergey


On Mon, Mar 8, 2010 at 10:54 PM, Robert Roland <rr...@shopzilla.com>wrote:

>
> Hi Sergey,
>
> Thanks for replying so quickly!
>
> It seems to do the same with List<Long> - my ParameterHandler is never
> called.
>
> I'm using CXF 2.2.6.  If you need any additional detail, I can email you
> off
> list so I'm not putting large chunks of code on the mailing list.
>
> Thanks much,
>
> Rob
>
>
> Sergey Beryozkin-5 wrote:
> >
> > Hi Rob
> >
> > Perhaps there's a big in ProviderFactory to do with finding
> > ParameterHandler
> > parameterized with array classes like Long[]...I'll look into it, in
> > meantime you might want to try List<Long> instead...
> >
> > cheers, Sergey
> >
> > On Mon, Mar 8, 2010 at 7:47 PM, Robert Roland <rr...@shopzilla.com>
> > wrote:
> >
> >>
> >> Hi all,
> >>
> >> I'm attempting to make my service accept a URL like:
> >>
> >> /getEffectiveBid?offerIds=1;2;3;4
> >>
> >> I know that I can use a Long[] for a URL like:
> >>
> >> /getEffectiveBid?offerIds=1&offerIds=2&offerIds=3
> >>
> >> However - this URL format is not acceptable.  I want to support a
> >> semicolon
> >> delimited list.
> >>
> >> I've tried registering a ParameterHandler:
> >>
> >>    <jaxrs:providers>
> >>      <bean
> >>
> class="com.shopzilla.ms.bidding.service.util.LongArrayParameterHandler"/>
> >>    </jaxrs:providers>
> >>
> >> My method looks like this:
> >>
> >>    @Override
> >>    @Path("getEffectiveBid")
> >>    @Produces( { MediaType.APPLICATION_XML, "application/fastinfoset",
> >> MediaType.APPLICATION_JSON })
> >>    @GET
> >>    public GetEffectiveBidResponse
> getEffectiveBid(@QueryParam("offerIds")
> >> Long[] offerIds) throws Exception {
> >>
> >> My ParameterHandler is never called, and I get the following error:
> >>
> >> Mar 8, 2010 11:45:29 AM org.apache.cxf.jaxrs.utils.InjectionUtils
> >> handleParameter
> >> SEVERE: Class java.lang.Long can not be instantiated using a constructor
> >> with a single String argument
> >>
> >> How do I do this?  I've tried looking at the mailing lists, but it seems
> >> everyone gives up and uses a String parameter, which just seems...
> dirty.
> >> :)
> >>
> >> Thanks much for your help,
> >>
> >> Rob
> >> --
> >> View this message in context:
> >>
> http://old.nabble.com/ParameterHandler-for-a-Long-array-tp27826543p27826543.html
> >> Sent from the cxf-user mailing list archive at Nabble.com.
> >>
> >>
> >
> >
>
> --
> View this message in context:
> http://old.nabble.com/ParameterHandler-for-a-Long-array-tp27826543p27828936.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>
>

Re: ParameterHandler for a Long array

Posted by Robert Roland <rr...@shopzilla.com>.
Hi Sergey,

Thanks for replying so quickly!

It seems to do the same with List<Long> - my ParameterHandler is never
called.

I'm using CXF 2.2.6.  If you need any additional detail, I can email you off
list so I'm not putting large chunks of code on the mailing list.

Thanks much,

Rob


Sergey Beryozkin-5 wrote:
> 
> Hi Rob
> 
> Perhaps there's a big in ProviderFactory to do with finding
> ParameterHandler
> parameterized with array classes like Long[]...I'll look into it, in
> meantime you might want to try List<Long> instead...
> 
> cheers, Sergey
> 
> On Mon, Mar 8, 2010 at 7:47 PM, Robert Roland <rr...@shopzilla.com>
> wrote:
> 
>>
>> Hi all,
>>
>> I'm attempting to make my service accept a URL like:
>>
>> /getEffectiveBid?offerIds=1;2;3;4
>>
>> I know that I can use a Long[] for a URL like:
>>
>> /getEffectiveBid?offerIds=1&offerIds=2&offerIds=3
>>
>> However - this URL format is not acceptable.  I want to support a
>> semicolon
>> delimited list.
>>
>> I've tried registering a ParameterHandler:
>>
>>    <jaxrs:providers>
>>      <bean
>> class="com.shopzilla.ms.bidding.service.util.LongArrayParameterHandler"/>
>>    </jaxrs:providers>
>>
>> My method looks like this:
>>
>>    @Override
>>    @Path("getEffectiveBid")
>>    @Produces( { MediaType.APPLICATION_XML, "application/fastinfoset",
>> MediaType.APPLICATION_JSON })
>>    @GET
>>    public GetEffectiveBidResponse getEffectiveBid(@QueryParam("offerIds")
>> Long[] offerIds) throws Exception {
>>
>> My ParameterHandler is never called, and I get the following error:
>>
>> Mar 8, 2010 11:45:29 AM org.apache.cxf.jaxrs.utils.InjectionUtils
>> handleParameter
>> SEVERE: Class java.lang.Long can not be instantiated using a constructor
>> with a single String argument
>>
>> How do I do this?  I've tried looking at the mailing lists, but it seems
>> everyone gives up and uses a String parameter, which just seems... dirty.
>> :)
>>
>> Thanks much for your help,
>>
>> Rob
>> --
>> View this message in context:
>> http://old.nabble.com/ParameterHandler-for-a-Long-array-tp27826543p27826543.html
>> Sent from the cxf-user mailing list archive at Nabble.com.
>>
>>
> 
> 

-- 
View this message in context: http://old.nabble.com/ParameterHandler-for-a-Long-array-tp27826543p27828936.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: ParameterHandler for a Long array

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi Rob

Perhaps there's a big in ProviderFactory to do with finding ParameterHandler
parameterized with array classes like Long[]...I'll look into it, in
meantime you might want to try List<Long> instead...

cheers, Sergey

On Mon, Mar 8, 2010 at 7:47 PM, Robert Roland <rr...@shopzilla.com> wrote:

>
> Hi all,
>
> I'm attempting to make my service accept a URL like:
>
> /getEffectiveBid?offerIds=1;2;3;4
>
> I know that I can use a Long[] for a URL like:
>
> /getEffectiveBid?offerIds=1&offerIds=2&offerIds=3
>
> However - this URL format is not acceptable.  I want to support a semicolon
> delimited list.
>
> I've tried registering a ParameterHandler:
>
>    <jaxrs:providers>
>      <bean
> class="com.shopzilla.ms.bidding.service.util.LongArrayParameterHandler"/>
>    </jaxrs:providers>
>
> My method looks like this:
>
>    @Override
>    @Path("getEffectiveBid")
>    @Produces( { MediaType.APPLICATION_XML, "application/fastinfoset",
> MediaType.APPLICATION_JSON })
>    @GET
>    public GetEffectiveBidResponse getEffectiveBid(@QueryParam("offerIds")
> Long[] offerIds) throws Exception {
>
> My ParameterHandler is never called, and I get the following error:
>
> Mar 8, 2010 11:45:29 AM org.apache.cxf.jaxrs.utils.InjectionUtils
> handleParameter
> SEVERE: Class java.lang.Long can not be instantiated using a constructor
> with a single String argument
>
> How do I do this?  I've tried looking at the mailing lists, but it seems
> everyone gives up and uses a String parameter, which just seems... dirty.
> :)
>
> Thanks much for your help,
>
> Rob
> --
> View this message in context:
> http://old.nabble.com/ParameterHandler-for-a-Long-array-tp27826543p27826543.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>
>