I have a Spring-MVC
@RestController
that uses generic names rather than the names I have configured with @XmlRootElement
or @JacksonXmlRootElement
. I want XML
to look like this:
<list>
<foo>
<name>John</name>
</foo>
</list>
but I get the following:
<ArrayList>
<item>
<name>John</name>
</item>
</ArrayList>
Marshalling a single instance correctly looks like this:
<foo>
<name>John</name>
</foo>
To try and solve this, I've tried using both Jackson
and JAXB
annotations. I've also conducted an extensive search for someone else's solution on Stack Overflow, various blogs, and issues reported against Jackson
and Spring-mvc
.
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
public class JacksonXmlTest {
@XmlRootElement(name="foo")
@XmlAccessorType(XmlAccessType.FIELD)
public static class Foo {
private String name;
public Foo(String name) {
setName(name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Test
public void understandListTest() throws JsonProcessingException {
// This is a JUnit test.....
List<Foo> fooList = new ArrayList<>();
fooList.add(new Foo("John"));
XmlMapper mapper = new XmlMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.registerModule(new JaxbAnnotationModule());
System.err.println(mapper.writeValueAsString(fooList));
System.err.println();
System.err.println(mapper.writeValueAsString(fooList.get(0)));
}
}
Please help me configure jackson to output the list wrapped in "list" tags and have each Foo object contained in "foo" tags rather than "item" tags.