cxf-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From subh <sube...@aol.com>
Subject RE: Passing array as a parameter in JAX-RS
Date Wed, 25 Mar 2009 16:39:10 GMT

Hi,

I got it working. I forgot to change the @PathParam to String.

Thanks for your help.

Rgds
Subh


subh wrote:
> 
> Hi Sergey,
> 
> Thanks for your reply.
> 
> I tried the ParameterHandler way by creating a ArrayParamHandler class for
> my Integer[].
> 
> public Integer[] fromString(String ids)   {
>       String[] idsArr = ids.split(",");
>       return getIntArr(idsArr);
> }
> 
> private Integer[] getIntArr(String[] arrStr)  {
>       Integer[] arrInt = new Integer[arrStr.length];
>       int i = 0;
>       for (int k = 0; k < arrStr.length; k++)   {
>          try  {
>             arrInt[i] = Integer.valueOf(arrStr[k]);
>             i++;
>          }
>          catch (Exception e)  {
>             log.error(e.getMessage());
>          }
>       }
>       return arrInt;
>    }
> 
> But when I make a call like 
> 
> http://localhost:8080/rs/masterData/address/ids/6679,6689 
> 
> I am getting the following error
> 
> ##ERROR##
> Mar 25, 2009 10:29:44 AM org.apache.cxf.jaxrs.utils.InjectionUtils
> reportServerError
> SEVERE: Parameter Class [Ljava.lang.Integer; has no constructor with
> single String parameter, static valueOf(String) or
> fromString(String) methods
> Mar 25, 2009 10:29:44 AM
> org.apache.cxf.interceptor.LoggingOutInterceptor$LoggingCallback onClose
> 
> What am I doing wrong?
> 
> Rgds
> Subh
> 
> 
> Sergey Beryozkin-2 wrote:
>> 
>> Hi
>> 
>> How such an array is represented on the wire ?
>> I reckon we can easily support values separated by space or indeed by
>> some other separators which can be captured by a regular expression.
>> 
>> But at the moment you need to do some additional work.
>> If it's XML then you might be able to use XmlJavaTypeAdapter [1]
>> Otherwise you need to create a MessageBodyReader implementation, see for
>> some samples at [2]
>> 
>> Actually, just looked again at your example :
>> 
>> If these ids are captured by queries like ?id=1&id=2&id=3
>> or matrix values /ids;id=1;id=2;id=3 then you can do either 
>> 
>> @QueryParam("id") List<Integer> ids
>> or 
>> @MatrixParam("id") List<Integer> ids
>> 
>> Though it would be nice if we supported arrays in such cases too.
>> 
>> In your example you use @PathParam so it looks like the sequence is
>> presented in one of the path segments like this : /ids/1+2+3+4+5
>> 
>> I think in this case you can register an instance of ParameterHandler,
>> see the section at [3],
>> which is a CXF extension and convert the string into the integer array 
>> 
>> Cheers, Sergey
>> 
>> [1]
>> http://weblogs.java.net/blog/kohsuke/archive/2005/09/using_jaxb_20s.html
>> 
>> [2]
>> http://cwiki.apache.org/CXF20DOC/jax-rs.html#JAX-RS-CustomMessageBodyPro
>> viders
>> [3]
>> http://cwiki.apache.org/CXF20DOC/jax-rs.html#JAX-RS-DealingwithParameter
>> s
>> 
>> -----Original Message-----
>> From: subh [mailto:subendu@aol.com] 
>> Sent: 24 March 2009 21:18
>> To: users@cxf.apache.org
>> Subject: Passing array as a parameter in JAX-RS
>> 
>> 
>> I am new to JAX-RS and was trying to implement a simple service. 
>> 
>> But I am not sure how to go about sending an array into the REST URI for
>> the
>> below code
>> 
>>    @GET
>>    @Path ("/address/ids/{addressIds}")
>>    public AddressDetailCollection getAddressById(@PathParam
>> ("addressIds") 
>> Integer[] ids)
>>    {
>>       ......
>>    }
>> 
>> 
>> -- 
>> View this message in context:
>> http://www.nabble.com/Passing-array-as-a-parameter-in-JAX-RS-tp22690005p
>> 22690005.html
>> Sent from the cxf-user mailing list archive at Nabble.com.
>> 
>> 
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Passing-array-as-a-parameter-in-JAX-RS-tp22690005p22705790.html
Sent from the cxf-user mailing list archive at Nabble.com.


Mime
View raw message