Question

I seem to be experiencing a problem when using Jackson to serialize to XML. My code is below:

TEST CONTAINER

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(tc)); tc = xm.readValue(sb.toString(), TestContainer.class);

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

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

but I'm getting:

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

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

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

I'm pretty sure this is something stupid on my part, but any help would be most appreciated.

Was it helpful?

Solution 2

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

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

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

OTHER TIPS

Ok, couple of notes. First, @JsonRootName only affects name used for the root of XML document, as name implies. So it is not used for TestChild. Second, it sounds like you want to use so-called "unwrapped" output for Lists, omitting element for property that contains List elements. This is doable with:

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

since default setting is to use wrapper (this is different from JAXB, where unwrapped is the default). Or, if you want to change this globally to assume unwrapped as default, you can change the defaults via XmlModule:

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

Hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top