在 java fasterxml 中创建 Xml

Create Xml in java fasterxml

我正在使用 fasterxml.jackson 通过 POJO 创建 xml。

我期望输出如下:

<Customer xmlns="">
    <firstname>FirstNameA</firstname>
    <middlename>LastNameA</middlename>
</Customer>

But getting output as:
<Customer xmlns="">
    <item>
        <firstname>FirstNameA</firstname>
        <middlename>LastNameA</middlename>
    </item>
</Customer>

为什么要附加 <item> 标签。我没有在任何地方附加 <item> 标签,但输出显示 <item> tag.what 在我的代码中是错误的?

这是 POJO

    @JacksonXmlRootElement(localName = "Customer")
    public class Customer {  
        @JacksonXmlProperty(localName="firstname")
        private String firstname;

        @JacksonXmlProperty(localName="middlename")
        private String middlename;


        public Customer(String firstname, String middlename) {
            this.firstname = firstname;
            this.middlename = middlename;
        }
}

// code to create xml
    ObjectMapper xmlMapper = new XmlMapper();
            JacksonXmlModule module = new JacksonXmlModule();
            module.setDefaultUseWrapper(false);
            Customer[] cust = new Customer[]{new Customer("FirstNameA", "LastNameA")};
            try {
                String xml = xmlMapper.writeValueAsString(cust);
                return xml;
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }

您代码中的 customer 是一个数组,您发布的 xml 样式中不能添加多个项目:

<Customer xmlns="">
    <firstname>FirstNameA</firstname>
    <middlename>LastNameA</middlename>
</Customer>

只需在您的代码中替换:

Customer cust = new Customer("FirstNameA", "LastNameA");