JAXB - 将 SOAP 映射到 Java 类 [英] JAXB - Mapping SOAP to Java Classes

查看:43
本文介绍了JAXB - 将 SOAP 映射到 Java 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助将我的 Soap Envelope 映射到 Java 类,我打算将结果操作到 DB.

I need help to mapping my Soap Envelope to java Classes, my intention manipulate the results to DB.

我在获取 SOAP Envelope 或使用 DB 时没有任何问题,我的问题完全在于 JABX 并根据我的 SOPA Envoloap 映射我的类.

I dont't have problems with get my SOAP Envelope or to work With DB, my problems is totaly with JABX and mapping my classes according my 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>

这是我的类,接收主要值(仪表板 DTO 的计数、返回和列表):

This is my the Class with receive the main values (count, return and list of 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 的第二个模型:

This is the second model that receives the 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 转换为结果,但出现错误:

And my app try to convert the SOAPEnvelope to a Result but I got error:

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>

我做错了什么?

推荐答案

试试这个:

首先你有你的根类,响应

First you have your root class, 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 类:

Now it get's a bit "messy" for my taste. Your xml contains an element DashboardDTOs which inside it contains a list of DashboardDTOs and those have value, code, names. So you need to create a DashboardDTOs class like this:

@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 编组/解组.

These POJOs will allow you to marshal/unmarshal with the specified xml inside the body.

更新以回复评论:

使用更新的 xml,类将如下所示:

With the updated xml, the classes would look like:

@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 文件(为了方便):

Now using an xml file (for convenience):

<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>

并尝试编组/解组是否在一个简单的 main 中工作:

And trying if the marshaling/unmarshaling works in a simple main:

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();
    }

}

对我来说很好用.在主要尝试这个,如果它有效,但它不会在你的应用程序中,让我知道,我们可以看看那里还有什么问题.

works fine for me. Try this in the main and if it works, but then it does not in your application, let me know and we can see what else can be wrong there.

这篇关于JAXB - 将 SOAP 映射到 Java 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆