Jackson XML globally set element name for container types

Posted by maxenglander on Stack Overflow See other posts from Stack Overflow or by maxenglander
Published on 2012-10-15T21:24:01Z Indexed on 2012/10/15 23:02 UTC
Read the original article Hit count: 487

Filed under:
|
|
|

I'm using Jackson 1.9.2 with the XML databind module. I need to tweak the way that Jackson serializes arrays, lists, collections.

By default, with an int array property called myProperty containing a couple numbers, Jackson / XML is producing the following:

<myProperty>
  <myProperty>1</myProperty>
  <myProperty>2</myProperty>
</myProperty>

What I need to produce is:

<myProperty>
  <item>1</item>
  <item>2</item>
</myProperty>

I can do this on a per-POJO basis using a combination of JacksonXmlElementWrapper and JacksonXmlProperty like so:

@JacksonXmlElementWrapper(localname='myProperty')
@JacksonXmlProperty(localname='item')
public int[] myProperty;

This solution, however, would require that I manually apply these annotations to every array, list, collection in my POJOs. A much better solution would allow me to apply a solution once, globally, for all array, list, collection types. Any ideas on how to implement such a solution?

Thanks!

© Stack Overflow or respective owner

Related posts about java

Related posts about Xml