You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by fthouny <fa...@gmail.com> on 2014/01/17 19:24:36 UTC

[JAX-RS] Customize wrapper pluralization

Hi,

i just migrated to CXF and I need to know how i can customize the
pluralization done by CXF to obtain the wrapper name when returning a
collection. For example, "country" is pluralized to "countrys" and i need it
to be "countries".
Please see my use case below.

@Path("/countries")
public class SupportedCountriesResource {
    @GET
    List<Country> list() {
       ...
    }
}

@XmlRootElement(name = "country")
@XmlAccessorType(XmlAccessType.FIELD)
public class Country {
    @XmlElement(name = "code")
    private String code;

    @XmlElement(name = "name")
    private String name;

    ...
}

I need to have the following xml and json (using
org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider) outputs :

<?xml version="1.0" encoding="UTF-8"?>
<countries>
   <country>
      <code>AF</code>
      <name>Afghanistan</name>
   </country>
   <country>
      <code>AL</code>
      <name>Albania</name>
   </country>
   ...
</countries>

[
   {
      "code":"AF",
      "name":"Afghanistan"
   },
   {
      "code":"AL",
      "name":"Albania"
   }
   ...
]

Thanks for your help,

Fabien



--
View this message in context: http://cxf.547215.n5.nabble.com/JAX-RS-Customize-wrapper-pluralization-tp5738743.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: [JAX-RS] Customize wrapper pluralization

Posted by fthouny <fa...@gmail.com>.
That's exactly what i was looking for, i missed it in the doc.

Thanks a lot Sergey !

@Jose : yes it's also work but there are some drawbacks :
1/ we lose the explicit method signature (i.e. we don't know that Countries
wraps a collection )
2/ it also adds a wrapper in the json output which is not acceptable here.

Anyway, thanks for your help !

Fabien 

 



--
View this message in context: http://cxf.547215.n5.nabble.com/JAX-RS-Customize-wrapper-pluralization-tp5738743p5738813.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: [JAX-RS] Customize wrapper pluralization

Posted by Sergey Beryozkin <sb...@gmail.com>.
Not really, it is an explicit collection so the wrapper needs to be 
customized,
When using JSONProvider, see
http://cxf.apache.org/docs/jax-rs-data-bindings.html#JAX-RSDataBindings-JSONsupport, 
"Handling Explicit collections"

HTH, Sergey


On 17/01/14 19:29, Jose María Zaragoza wrote:
> 2014/1/17 fthouny <fa...@gmail.com>:
>
>> I need to have the following xml and json (using
>> org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider) outputs :
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <countries>
>>     <country>
>>        <code>AF</code>
>>        <name>Afghanistan</name>
>>     </country>
>>     <country>
>>        <code>AL</code>
>>        <name>Albania</name>
>>     </country>
>>     ...
>> </countries>
>>
>> [
>>     {
>>        "code":"AF",
>>        "name":"Afghanistan"
>>     },
>>     {
>>        "code":"AL",
>>        "name":"Albania"
>>     }
>>     ...
>> ]
>>
>> Thanks for your help,
>>
>> Fabien
>>
>
> Hello:
>
> I guess that you need to return a Countries object with
> @XmlRootElement(name = "countries"), don't you ?
>
> Regards
>



Re: [JAX-RS] Customize wrapper pluralization

Posted by Jose María Zaragoza <de...@gmail.com>.
2014/1/17 fthouny <fa...@gmail.com>:

> I need to have the following xml and json (using
> org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider) outputs :
>
> <?xml version="1.0" encoding="UTF-8"?>
> <countries>
>    <country>
>       <code>AF</code>
>       <name>Afghanistan</name>
>    </country>
>    <country>
>       <code>AL</code>
>       <name>Albania</name>
>    </country>
>    ...
> </countries>
>
> [
>    {
>       "code":"AF",
>       "name":"Afghanistan"
>    },
>    {
>       "code":"AL",
>       "name":"Albania"
>    }
>    ...
> ]
>
> Thanks for your help,
>
> Fabien
>

Hello:

I guess that you need to return a Countries object with
@XmlRootElement(name = "countries"), don't you ?

Regards