Search code examples
javaxmljacksonxml-serializationfasterxml

FasterXML serialization without using field type in XML


I'm trying to achieve following XML:

<model>
    <entry>
        <key>A</key>
        <value>1</value>
    </entry>
    <entry>
        <key>B</key>
        <value>2</value>
    </entry>
</model>

Closest POJO model I've got by experimenting with my code looks like this:

import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import static com.google.common.collect.Lists.newArrayList;
import java.util.ArrayList;
import java.util.List;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import org.junit.Test;

public class InsPersonFormTest {

    @JsonRootName("model")
    public static class Model extends ArrayList<Entry> {

        public Model(List<Entry> entries) { super(entries); }

        public List<Entry> getEntry() { return this; }
    }

    public static class Entry {

        String key;
        String value;

        public Entry(String key, String value) {
            this.key = key;
            this.value = value;
        }

        public String getKey() { return key; }

        public String getValue() { return value; }       
    }

    @Test
    public void shouldSendPostRequest() throws JsonProcessingException {
        Model model = new Model(newArrayList(new Entry("A", "1"), new Entry("B", "2")));

        ObjectMapper xmlMapper = new XmlMapper();
        String xml = xmlMapper.writeValueAsString(model);

        assertThat(xml, equalTo(
                "<model>"
                + "<entry><key>A</key><value>1</value></entry>"
                + "<entry><key>B</key><value>2</value></entry>"
                + "</model>"));
    }
}

But it gives me

Expected:

"<model><entry><key>A</key><value>1</value></entry><entry><key>B</key><value>2</value></entry></model>"

but: was

"<model><item><key>A</key><value>1</value></item><item><key>B</key><value>2</value></item></model>"

How can I change item to entry or use simpliest POJO structure with more suitable Map<String, String> field?


Solution

  • Don't make your Model class a subtype of ArrayList. Instead use composition

    public static class Model {
        private ArrayList<Entry> entry;
    
        public Model(List<Entry> entries) {
            entry = entries; // or make a copy 
        }
    
        @JacksonXmlElementWrapper(useWrapping = false)
        public List<Entry> getEntry() {
            return entry
        }
    
    }
    

    ArrayList is a List and Jackson handles List in a managed way.

    You'll need to add JacksonXmlElementWrapper so you can tell Jackson not to wrap the resulting XML.

    You can then use

    @JacksonXmlRootElement(/* custom */)
    

    to annotate Entry and add local name of the XML node and namespace value.