jhamm jhamm - 8 days ago 6
Java Question

Jackson List Help - Java

I am trying to create some xml with Jackson and I cannot get the list to display like I need. I am getting:

<Messages>
<Messages>...</Messages>
<Messages>...</Messages>
</Messages>


I want it to look like:

<Messages>
<Message>...</Message>
<Message>...</Message>
</Messages>


My code looks like this:

public List<Message> messages;


Whatever I name that variable, is the same name all of the child elements get. I am sure this has been answered elsewhere, but I cannot find anything that will take care of my issue. Thanks for the help.

Answer

I found the easy way to do this without adding more dependencies. You just use the annotations:

@JacksonXmlElementWrapper(localName = "Messages")
@JacksonXmlProperty(localName = "Message")

This question is what pointed me in the right direction. Jackson XML globally set element name for container types. You can also read about this annotation on the github page here

Comments