JAXB - 将SOAP映射到Java类

时间:2018-01-31 22:28:48

标签: soap jaxb unmarshalling

我需要帮助将我的Soap Envelope映射到java Classes,我的意图是将结果操作到DB。

我在获取SOAP信封或使用DB时遇到问题,我的问题完全在于JABX并根据我的SOPA Envoloap映射我的类。

这是我的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>

这是我的Class,它接收主要值(计数,返回和Dashboards 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>

我做错了什么?

Thans

1 个答案:

答案 0 :(得分:3)

试试这个:

首先你有你的根类,响应

@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;
}

现在它有点&#34;凌乱&#34;为了我的口味。您的xml包含一个元素DashboardDTOs,其中包含DashboardDTO列表,其中包含值,代码和名称。所以你需要像这样创建一个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();
    }

}

对我来说很好。在main中尝试这个,如果它有效,但它不在你的应用程序中,让我知道,我们可以看到那里还有什么可能是错的。