Spring boot 有可能让Jackson ObjectMapper在Spring引导应用程序中遵守JAXB XML注释吗?

Spring boot 有可能让Jackson ObjectMapper在Spring引导应用程序中遵守JAXB XML注释吗?,spring-boot,jackson,jaxb,java,xml,Spring Boot,Jackson,Jaxb,Java,Xml,让应用程序使用ObjectMapper我使用以下模式将列表字段很好地序列化: @JacksonXmlProperty(localName = "item") @JacksonXmlElementWrapper(useWrapping = true, localName = "items") private List<Item> items; 如需弹簧千斤顶配置和注释,请执行以下操作: @XmlElementWrapper @XmlElement(name="item") p

让应用程序使用
ObjectMapper
我使用以下模式将
列表
字段很好地序列化:

@JacksonXmlProperty(localName = "item")
@JacksonXmlElementWrapper(useWrapping = true, localName = "items")    
private List<Item> items;
如需弹簧千斤顶配置和注释,请执行以下操作:

@XmlElementWrapper
@XmlElement(name="item")
private List<Item> items;
@xmlementwrapper
@xmlement(name=“item”)
私人清单项目;
没有按预期工作。我目前正在调查是否有可能像年一样用GSON替换杰克逊

我真的希望这个注释是通用的,而不仅仅是特定的,或者像为每个序列化程序实现添加注释一样。

我还没有尝试过,也不确定它是否与spring boot兼容(如果兼容,您应该写回),但值得测试它

更新

这是一个测试(使用上面的链接)

import java.io.IOException;
导入java.io.StringWriter;
导入java.util.array;
导入java.util.List;
导入javax.xml.bind.annotation.XmlAccessType;
导入javax.xml.bind.annotation.XmlAccessorType;
导入javax.xml.bind.annotation.xmlement;
导入javax.xml.bind.annotation.XmlRootElement;
导入javax.xml.stream.XMLOutputFactory;
导入javax.xml.stream.XMLStreamException;
导入javax.xml.stream.XMLStreamWriter;
导入com.fasterxml.jackson.databind.ObjectMapper;
导入com.fasterxml.jackson.dataformat.xml.XmlMapper;
导入com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
公共类TestJacksonWithJaxbAnnot{
静态ObjectMapper ObjectMapper=新ObjectMapper();
静止的{
registerModule(新的JaxbAnnotationModule());
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name=“item”)
静态类项{
@XmlElement
int-id;
@XmlElement
字符串名;
//能手、二传手等。
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name=“itemcollection”)
静态类ItemCollection{
@XmlElement
项目的字符串名称;
@XmlElement
清单项目;
//能手、二传手等。
}
公共静态void main(字符串[]args)抛出XMLStreamException、IOException{
ItemCollection testColl=新的ItemCollection();
testColl.nameOfItems=“测试项目”;
Item item1=新项();
项目1.id=1;
item1.name=“苹果”;
Item item2=新项();
项目2.id=2;
item2.name=“橙色”;
testColl.items=Arrays.asList(item1,item2);
StringWriter StringWriter=新StringWriter();
XMLOutputFactory XMLOutputFactory=XMLOutputFactory.newInstance();
XMLStreamWriter sw=xmlOutputFactory.createXMLStreamWriter(stringWriter);
XmlMapper mapper=新的XmlMapper();
sw.writeStartDocument();
sw.WriteStarteElement(“根”);
mapper.writeValue(sw、testColl);
sw.writeComment(“这里有一些有见地的评论”);
sw.writeedelement();
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>

测试项目
1.
苹果
2.
橙色
如您所见,它并不完美,例如,它没有处理@XmlRootElement注释中定义的名称。(在我的1.5.10.RELEASE spring boot项目中进行了测试,其中jackson dataformat xml具有2.8.10托管版本)


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

谢谢您的提示。尽管我认为没有人会将此标记为“仅链接答案”(更适合作为评论)。我检查了我正在使用的项目的配置,它实际上包括jackson dataformat xml的2.9.5版,我将进一步检查您创建的链接是否提供了如何让ObjectMapper真正使用该功能的详细信息。我通常会为此创建两个不同的DTO,因为很容易发现,您需要对JSON/XML使用不同的表示,在这种情况下,您实现简单化/集中化等的努力将是徒劳的,除此之外,JAXB/Jackson for JSON的这一特性并不为人所知,也许这并不是人们所期望的战斗证明。您提供的
列表的输出是默认输出,因此没有任何注释。我将很快更新我的问题。
@XmlElementWrapper
@XmlElement(name="item")
private List<Item> items;
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>