是否可以让 Jackson ObjectMapper 在 Spring Boot 应用程序中遵守 JAXB XML 注释



有一个使用 jackson 的 spring-boot 应用程序ObjectMapper我使用以下模式很好地序列化List字段:

@JacksonXmlProperty(localName = "item")
@JacksonXmlElementWrapper(useWrapping = true, localName = "items")    
private List<Item> items;

所以得到这样的东西:

<items>
<item>...</item>
<item>...</item>
...
</items>

我已经研究了 - 没有任何运气 - 如何使用像@XmlElementWrapper这样的jaxb注释来实现这一点。

例如,我可以用JacksonConfiguration做什么吗?

更新

添加

objectMapper.registerModule(new JaxbAnnotationModule());

到弹簧杰克逊配置和注释,如:

@XmlElementWrapper
@XmlElement(name="item")
private List<Item> items;

无法按预期工作。我目前正在研究用GSON替换杰克逊的可能性,如这篇文章中所述

我真的很想让这个注释的东西是通用的,所以不仅仅是杰克逊特定的,或者喜欢为每个序列化程序实现添加注释。

我没有尝试过jackson-dataformat-xml/JaxbAnnotationModule,我不确定它是否与spring-boot兼容(如果是,你应该回信(,但值得测试它。

更新

在这里,您是一个测试(使用上面的链接(

import java.io.IOException;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;

public class TestJacksonWithJaxbAnnot {
static ObjectMapper objectMapper = new ObjectMapper();
static  {
objectMapper.registerModule(new JaxbAnnotationModule());
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "item")
static class Item {
@XmlElement
int id;
@XmlElement
String name;
//getters, setters, etc.
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "itemcollection")
static class ItemCollection {
@XmlElement
String nameOfItems;
@XmlElement
List<Item> items;
//getters, setters, etc.
}
public static void main(String[] args) throws XMLStreamException, IOException {
ItemCollection testColl = new ItemCollection();
testColl.nameOfItems = "Test items";
Item item1 = new Item();
item1.id = 1;
item1.name = "apple";
Item item2 = new Item();
item2.id = 2;
item2.name = "orange";
testColl.items = Arrays.asList(item1, item2);
StringWriter stringWriter = new StringWriter();
XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
XMLStreamWriter sw =     xmlOutputFactory.createXMLStreamWriter(stringWriter);
XmlMapper mapper = new XmlMapper();
sw.writeStartDocument();
sw.writeStartElement("root");
mapper.writeValue(sw, testColl);
sw.writeComment("Some insightful commentary here");
sw.writeEndElement();
sw.writeEndDocument();
System.out.println(stringWriter.toString());
}
}

输出:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<ItemCollection>
<nameOfItems>Test items</nameOfItems>
<items>
<items>
<id>1</id>
<name>apple</name>
</items>
<items>
<id>2</id>
<name>orange</name>
</items>
</items>
</ItemCollection>
<!--Some insightful commentary here-->
</root>

如您所见,它并不完美,例如,它没有处理@XmlRootElement注释中定义的名称。(在我的 1.5.10.RELEASE spring-boot 项目中测试过,其中 jackson-dataformat-xml 有 2.8.10 托管版本(。

也许使用更高版本,您会获得更好的输出。

最新更新