JAXB - 将SOAP映射到Java类

3
我希望您能帮助我将SOAP信封映射到Java类,我的意图是将结果操作到数据库中。
获取SOAP信封或使用数据库没有问题,但是我完全不懂JABX和如何根据我的SOAP信封映射我的类,这是我的SOAP:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Header>
      <KD4SoapHeaderV2 xmlns="http://www.ibm.com/KD4Soap">A03ODA1YzhlZDQ2MWQAAQ==</KD4SoapHeaderV2>
   </soap:Header>
   <soap:Body>
      <Response xmlns="http://tempuri.org/">
         <Result xmlns:a="http://schemas.datacontract.org/2004/07/PS.SharedWebServices.Response" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <a:Errors />
            <a:Count>329</a:Count>
            <a:Return>SUCCESS</a:Return>
            <a:DashboardDTOs>
               <a:DashboardDTOs>
                  <a:Value>28.58</a:Value>
                  <a:Code>O001</a:Code>
                  <a:Name>Test2</a:Name>
               </a:DashboardDTOs>
               <a:DashboardDTOs>
                  <a:Value>40.22</a:Value>
                  <a:Code>O002</a:Code>
                  <a:Name>Test2</a:Name>
               </a:DashboardDTOs>
               <a:DashboardDTOs>
                  <a:Value>54.11</a:Value>
                  <a:Code>O003</a:Code>
                  <a:Name>Test3</a:Name>
               </a:DashboardDTOs>
            </a:DashboardDTOs>
         </Result>
      </Response>
   </soap:Body>
</soap:Envelope>

这是我的接收主要值(计数、返回值和仪表板DTO列表)的类:
@XmlRootElement(name = "Response") 
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Response", propOrder = { "Count",  "Return", "DashboardDTOs"})
public class Result {

    @XmlElement(name="Count", required = true)
    private Integer Count;

    @XmlElement(name="Return", required = true)
    private String Return;

    @XmlElement(name = "DashboardDTOs")
    private List<DashboardDTOs> DashboardDTOs;

    ...

这是第二个模型,接收DashboardDTO:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "DashboardDTOs", propOrder = {
        "Value",
        "Code",
        "Name"
    })
public class DashboardDTOs {

    @XmlElement(name = "Value")
    private double Value;

    @XmlElement(name = "Code")
    private String Code;

    @XmlElement(name = "Name")
    private String Name;

    ...

我的应用程序试图将SOAPEnvelope转换为Result,但是出现了错误:
Unmarshaller unmarshaller = JAXBContext.newInstance(Result.class).createUnmarshaller();
GetListSummarizedTransactionResultDTO returnValue = (Result)unmarshaller.unmarshal(soapMessagem.getSOAPBody().extractContentAsDocument());



unexpected element (uri:"http://tempuri.org/", local:"Response"). Expected elements are <{}Result>

我做错了什么?
谢谢。
1个回答

5

试试这个:

首先你有根类 Response

@XmlRootElement(name = "Response", namespace = "http://tempuri.org/")
@XmlAccessorType(XmlAccessType.FIELD)
public class Response {

    @XmlElement(name="Result", namespace = "http://tempuri.org/")
    private Result result;
}

其中包含结果:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Result", propOrder = { "Errors", "Count",  "Return", "DashboardDTOs"})
public class Result {

    @XmlElement(name="Errors", required = true, namespace = "http://schemas.datacontract.org/2004/07/PS.SharedWebServices.Response")
    private String Errors;

    @XmlElement(name="Count", required = true, namespace = "http://schemas.datacontract.org/2004/07/PS.SharedWebServices.Response")
    private Integer Count;

    @XmlElement(name="Return", required = true, namespace = "http://schemas.datacontract.org/2004/07/PS.SharedWebServices.Response")
    private String Return;

    @XmlElement(name = "DashboardDTOs", namespace = "http://schemas.datacontract.org/2004/07/PS.SharedWebServices.Response")
    private List<DashboardDTOs> DashboardDTOs;
}

现在,对于我来说,事情变得有点“混乱”。您的XML包含一个名为DashboardDTOs的元素,其中包含一组DashboardDTOs,它们具有值,代码和名称。因此,您需要创建一个类DashboardDTOs,如下所示:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "DashboardDTOs", propOrder = {
        "Value",
        "Code",
        "Name",
        "DashboardDTOs"
})
public class DashboardDTOs {

    @XmlElement(name = "Value", namespace = "http://schemas.datacontract.org/2004/07/PS.SharedWebServices.Response")
    private double Value;

    @XmlElement(name = "Code", namespace = "http://schemas.datacontract.org/2004/07/PS.SharedWebServices.Response")
    private String Code;

    @XmlElement(name = "Name", namespace = "http://schemas.datacontract.org/2004/07/PS.SharedWebServices.Response")
    private String Name;

    @XmlElement(name = "DashboardDTOs", namespace = "http://schemas.datacontract.org/2004/07/PS.SharedWebServices.Response")
    private List<DashboardDTOs> DashboardDTOs;
}

这些POJO将允许您使用指定的xml在正文中进行编组/解组。
更新以响应评论:
使用更新的xml,类将如下所示:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "DashboardDTOs")
public class DashboardDTOs {

    @XmlElement(name = "DashboardDTO", namespace = "http://schemas.datacontract.org/2004/07/PS.SharedWebServices.Response")
    private List<DashboardDTO> dashboardDTO;
}

并且:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {
        "value",
        "code",
        "Nnme",
})
public class DashboardDTO {
    @XmlElement(name = "Value", namespace = "http://schemas.datacontract.org/2004/07/PS.SharedWebServices.Response")
    private double value;

    @XmlElement(name = "Code", namespace = "http://schemas.datacontract.org/2004/07/PS.SharedWebServices.Response")
    private String code;

    @XmlElement(name = "Name", namespace = "http://schemas.datacontract.org/2004/07/PS.SharedWebServices.Response")
    private String Nnme;
}

现在使用一个XML文件(为了方便):
<Response xmlns="http://tempuri.org/">
    <Result xmlns:a="http://schemas.datacontract.org/2004/07/PS.SharedWebServices.Response" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <a:Errors />
        <a:Count>329</a:Count>
        <a:Return>SUCCESS</a:Return>
        <a:DashboardDTOs>
            <a:DashboardDTO>
                <a:Value>28.58</a:Value>
                <a:Code>O001</a:Code>
                <a:Name>Test2</a:Name>
            </a:DashboardDTO>
            <a:DashboardDTO>
                <a:Value>40.22</a:Value>
                <a:Code>O002</a:Code>
                <a:Name>Test2</a:Name>
            </a:DashboardDTO>
            <a:DashboardDTO>
                <a:Value>54.11</a:Value>
                <a:Code>O003</a:Code>
                <a:Name>Test3</a:Name>
            </a:DashboardDTO>
        </a:DashboardDTOs>
    </Result>
</Response>

尝试在简单的主程序中测试编组/解组是否正常:

public static void main(String[] args) {

    try {

        File file = new File("response.xml");
        JAXBContext jaxbContext = JAXBContext.newInstance(Response.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

        Response response = (Response) jaxbUnmarshaller.unmarshal(file);
        System.out.println(response);

    } catch (JAXBException e) {
        e.printStackTrace();
    }

}

这对我来说很好用。如果在主要的应用程序中尝试并且它有效,但在您的应用程序中无效,请告诉我,我们可以看看还有什么其他问题。


你好mart,谢谢你的帮助。我在下面回答你的问题以发布一个干净的代码,你能帮我吗? - Leonardo Machado
嗨@LeonardoMachado。我更新了我的回复。看看并告诉我是否仍有问题。 - martidis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接