Java 使用Jackson将POJO序列化为具有名称空间和嵌套元素的XML

Java 使用Jackson将POJO序列化为具有名称空间和嵌套元素的XML,java,xml,soap,jackson,Java,Xml,Soap,Jackson,我的目标是使用普通的旧Java对象创建有效的XML请求字符串。我遇到的问题是,我不能创建多个包装元素,也不能正确分配名称空间。我现在掌握的代码是: package test_ground; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXm

我的目标是使用普通的旧Java对象创建有效的XML请求字符串。我遇到的问题是,我不能创建多个包装元素,也不能正确分配名称空间。我现在掌握的代码是:

   package test_ground;

   import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
   import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
   import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

   import java.util.List;

   @JacksonXmlRootElement(localName = "Envelope", namespace = 
   "http://schemas.xmlsoap.org/soap/envelope/" )
   public class RateReqModel {
    @JacksonXmlProperty(namespace = "http://schemas.xmlsoap.org/soap/envelope/")
    private String Header;

    @JacksonXmlElementWrapper(localName = "GetCurrencyRates", namespace = "http://tempuri.org/")
    private List<String> RateDate;

    public RateReqModel(List<String> RateDate){
        this.RateDate = RateDate;
        }
    }
package-test\u-ground;
导入com.fasterxml.jackson.dataformat.xml.annotation.jacksonxmlementwrapper;
导入com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
导入com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
导入java.util.List;
@JacksonXmlRootElement(localName=“信封”,命名空间=
"http://schemas.xmlsoap.org/soap/envelope/" )
公共类费率模型{
@JacksonXmlProperty(命名空间=”http://schemas.xmlsoap.org/soap/envelope/")
私有字符串头;
@JacksonXmlElementWrapper(localName=“GetCurrencyRates”,命名空间=”http://tempuri.org/")
私人名单日期;
公共费率模型(列出费率日期){
this.RateDate=RateDate;
}
}
我从上面的脚本中得到了什么:

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
  <Header/>
  <wstxns1:GetCurrencyRates xmlns:wstxns1="http://tempuri.org/">
    <RateDate xmlns="">2021-03-29</RateDate>
  </wstxns1:GetCurrencyRates>
</Envelope>
<ns1:Envelope xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns2="http://tempuri.org/">
    <ns1:Header/>
    <ns1:Body>
        <ns2:GetCurrencyRates>
            <ns2:RateDate>2021-03-29</ns2:RateDate>
        </ns2:GetCurrencyRates>
    </ns1:Body>
</ns1:Envelope>

2021-03-29
我需要什么:

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
  <Header/>
  <wstxns1:GetCurrencyRates xmlns:wstxns1="http://tempuri.org/">
    <RateDate xmlns="">2021-03-29</RateDate>
  </wstxns1:GetCurrencyRates>
</Envelope>
<ns1:Envelope xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns2="http://tempuri.org/">
    <ns1:Header/>
    <ns1:Body>
        <ns2:GetCurrencyRates>
            <ns2:RateDate>2021-03-29</ns2:RateDate>
        </ns2:GetCurrencyRates>
    </ns1:Body>
</ns1:Envelope>

2021-03-29

正如您所看到的,所需的XML结构有2个名称空间,并在RateDate元素周围双包装,这正是我所寻求的。

我的目标是使用普通的旧Java对象创建有效的XML请求字符串。您可能需要创建更多的POJO来包装其他POJO,并将适当的对象转换为XML结构。只要付出足够的努力,这是可行的。问题是为什么来自POJO?关于努力部分你是对的。我快到了。为了回答您的问题,我正在探索序列化XML文件的方法,以便放心使用。剩下的唯一一件事是我无法更改GetCurrencyRates节点的localName,它是一个嵌套的@JacksonXmlRootElement。它必须是“tem:GetcurrencyRates”。您可能会发现,最简单的方法是首先生成Jackson选择生成的任何XML,然后使用XSLT转换将其转换为您想要的特定XML。