You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by subh <su...@aol.com> on 2009/03/24 22:18:28 UTC

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-tp22690005p22690005.html
Sent from the cxf-user mailing list archive at Nabble.com.


RE: Passing array as a parameter in JAX-RS

Posted by subh <su...@aol.com>.
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.


Re: Passing array as a parameter in JAX-RS

Posted by subh <su...@aol.com>.
Hi,

I am using cxf-bundle-jaxrs-2.2.

I tried registering it using

<jaxrs:providers>
      <bean id="intArrayProvider"
class="com.mycompany.services.rest.util.ArrayParamHandler"/>
</jaxrs:providers>

To make it work, I changed the method getAddressById () to take String
instead of Int[], and inside the method I am using ArrayParamHandler to
convert it to Int[] (using fromString(ids))

public AddressDetailCollection getAddressById(@PathParam ("addressIds")
String ids)

Rgds
Subh


Sergey Beryozkin-2 wrote:
> 
> Hi
> 
> What CXF version are you using ? Support for ParameterHandlers should be
> available in 2.1.4.
> How are you registering it ?
> 
> thanks, Sergey
> 
> 
> ----- Original Message ----- 
> From: "subh" <su...@aol.com>
> To: <us...@cxf.apache.org>
> Sent: Wednesday, March 25, 2009 3:38 PM
> Subject: RE: Passing array as a parameter in JAX-RS
> 
> 
>> 
>> 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-tp22690005p22704300.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-tp22690005p22724484.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: Passing array as a parameter in JAX-RS

Posted by Sergey Beryozkin <sb...@progress.com>.
Hi

What CXF version are you using ? Support for ParameterHandlers should be available in 2.1.4.
How are you registering it ?

thanks, Sergey


----- Original Message ----- 
From: "subh" <su...@aol.com>
To: <us...@cxf.apache.org>
Sent: Wednesday, March 25, 2009 3:38 PM
Subject: RE: Passing array as a parameter in JAX-RS


> 
> 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-tp22690005p22704300.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>

RE: Passing array as a parameter in JAX-RS

Posted by subh <su...@aol.com>.
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-tp22690005p22704300.html
Sent from the cxf-user mailing list archive at Nabble.com.


RE: Passing array as a parameter in JAX-RS

Posted by Sergey Beryozkin <sb...@progress.com>.
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.