Losing type element when serializing an object inside an ArrayList in XML

I seem to be having trouble using Jackson to serialize to XML. My code is below:

CONTAINER TEST

package com.test; import java.util.ArrayList; import com.fasterxml.jackson.annotation.JsonProperty; public class TestContainer { private String testContainerID; private String testContainerMessage; private ArrayList<TestChild> testContainerChildren; @JsonProperty("TestContainerID") public String getTestContainerID() { return testContainerID; } @JsonProperty("TestContainerID") public void setTestContainerID(String testContainerID) { this.testContainerID = testContainerID; } @JsonProperty("TestContainerMessage") public String getTestContainerMessage() { return testContainerMessage; } @JsonProperty("TestContainerMessage") public void setTestContainerMessage(String testContainerMessage) { this.testContainerMessage = testContainerMessage; } @JsonProperty("TestContainerChildren") public ArrayList<TestChild> getTestContainerChildren() { return testContainerChildren; } @JsonProperty("TestContainerChildren") public void setTestContainerChildren(ArrayList<TestChild> testContainerChildren) { this.testContainerChildren = testContainerChildren; } } 

Testchild

 package com.test; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonRootName; @JsonRootName(value="TestChild") public class TestChild { private String testChildID; private String testChildMessage; @JsonProperty("TestChildID") public String getTestChildID() { return testChildID; } @JsonProperty("TestChildID") public void setTestChildID(String testChildID) { this.testChildID = testChildID; } @JsonProperty("TestChildMessage") public String getTestChildMessage() { return testChildMessage; } @JsonProperty("TestChildMessage") public void setTestChildMessage(String testChildMessage) { this.testChildMessage = testChildMessage; } } 

USE

  • Serialization:

    XmlMapper xm = new XmlMapper (); TestContainer tc = xm.readValue (sb.toString (), TestContainer.class);

  • Deserialization:

    System.out.println (xm.writeValueAsString (dts)); tc = xm.readValue (sb.toString (), TestContainer.class);

What I am doing is loading an XML file from a folder in the classpath and putting the contents of the file in a StringBuffer. The problem is the generated XML for the collection of objects. When writing XML, I want something like:

 <TestContainerChildren><TestChild><...(Element Details)...></TestChild></TestContainerChildren> 

but I get:

 <TestContainerChildren><TestContainerChildren><...(Element Details)...><TestContainerChildren></TestContainerChildren> 

I'm not sure what I am missing here. I have no problem with the JSON part of serialization / deserialization, only XML. I tried using Jackson and JAXB annotations to disable packaging, I tried using the following annotations:

  • @JsonRootName
  • @JsonProperty
  • @JacksonXmlElementWrapper
  • @JacksonElement
  • @XmlElementWrapper
  • @XmlElement

I'm pretty sure this is something stupid of me, but any help would be greatly appreciated.

+4
source share
2 answers

I got this working using the following annotations above the variable declaration:

  • @JacksonXmlElementWrapper (localName = "[insert collection name]")
  • @JacksonXmlProperty (localName = "[insert collection name name]")

This was a simple case of RTFM, as it documented here .

+3
source

Ok, a couple of notes. Firstly, @JsonRootName only affects the name used for the root of the XML document, as the name implies. Therefore, it is not used for TestChild . Secondly, it looks like you want to use the so-called "expanded" output for lists, excluding an element for a property that contains List elements. This can be done using

 @JacksonXmlElementWrapper(useWrapping=false) @JsonProperty("TestContainerChildren") public ArrayList<TestChild> getTestContainerChildren() { ... } 

since the default parameter is to use the shell (this is different from JAXB, where it is deployed by default). Or, if you want to change this globally to assume that it is deployed by default, you can change the default values ​​through the XmlModule :

 JacksonXmlModule module = new JacksonXmlModule(); // to default to using "unwrapped" Lists: module.setDefaultUseWrapper(false); XmlMapper xmlMapper = new XmlMapper(module); 

Hope this helps!

+2
source

Source: https://habr.com/ru/post/1495142/


All Articles