java 将 Soap XML 响应转换为对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47333185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:34:57  来源:igfitidea点击:

Convert Soap XML response to Object

javaxmlsoapjaxb

提问by Ayo K

i'm new to working with SOAP API's

我是 SOAP API 的新手

I have a soap response from an API

我有一个来自 API 的肥皂响应

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<LoginResponse xmlns="http://test.org/ADMail_Service">
<LoginResult>
<ErrorMessage>Successful login</ErrorMessage>
<Status>true</Status>
</LoginResult>
</LoginResponse>
</soapenv:Body>
</soapenv:Envelope>

I'm trying to transform this into an object.

我正在尝试将其转换为一个对象。

From reading articles online I'm trying to use JAXB to do this, but my object is empty.

通过在线阅读文章,我尝试使用 JAXB 来执行此操作,但我的对象为空。

Here's the code for reading the response. I wrote the response to an xml file for test purposes:

这是读取响应的代码。出于测试目的,我将响应写入了 xml 文件:

try {
    XMLInputFactory xif = XMLInputFactory.newFactory();
    XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml"));
    xsr.nextTag(); // Advance to Envelope tag

    xsr.nextTag(); // Advance to Body tag
    xsr.nextTag();
    xsr.nextTag();


    JAXBContext jc = JAXBContext.newInstance(LoginResult.class);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    JAXBElement<LoginResult> je = unmarshaller.unmarshal(xsr, LoginResult.class);

    System.out.println(je.getName());
    System.out.println(je.getValue());
} catch (XMLStreamException e) {
    e.printStackTrace();
} catch (JAXBException e) {
    e.printStackTrace();
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

The LoginResultclass:

LoginResult类:

public class LoginResult {
    private String errorMessage;
    private String status;

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}

Thanks in advance!

提前致谢!

回答by Zibaire

you can use this code to retrieve a POJO, and also add an @XmlRootElement as header to your POJO.

您可以使用此代码来检索 POJO,还可以将 @XmlRootElement 作为标题添加到您的 POJO。

(I did'nt test the code below)

(我没有测试下面的代码)

XMLInputFactory xif = XMLInputFactory.newFactory();
        XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml"));
        xsr.nextTag(); // Advance to Envelope tag

        xsr.nextTag(); // Advance to Body tag
        xsr.nextTag();
        xsr.nextTag();

        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        StringWriter stringWriter = new StringWriter();
        transformer.transform(new StAXSource(xsr), new StreamResult(stringWriter));
        StringReader sr = new StringReader(stringWriter.toString());
        JAXBContext jaxbContext = JAXBContext.newInstance(LoginResult.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        LoginResult loginResult = (LoginResult) unmarshaller.unmarshal(sr);

EDIT :

编辑 :

I found a solution for you:

我为您找到了解决方案:

    @XmlRootElement(name = "LoginResult", namespace = "http://test.org/ADMail_Service")
@XmlAccessorType(XmlAccessType.FIELD)
public class LoginResult {
    @XmlElement(name = "ErrorMessage", namespace = "http://test.org/ADMail_Service")
    private String errorMessage;
    @XmlElement(name = "Status", namespace = "http://test.org/ADMail_Service")
    private String status;

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}


public static void main(String[] args) {
        try {
            XMLInputFactory xif = XMLInputFactory.newFactory();
            XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml"));
            xsr.nextTag(); // Advance to Envelope tag

            xsr.nextTag(); // Advance to Body tag
            xsr.nextTag();
            xsr.nextTag();


            JAXBContext jc = JAXBContext.newInstance(LoginResult.class);
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            JAXBElement<LoginResult> je = unmarshaller.unmarshal(xsr, LoginResult.class);

            System.out.println(je.getName());
            System.out.println(je.getValue());
            System.out.println(je.getValue().getErrorMessage());
        } catch (XMLStreamException e) {
            e.printStackTrace();
        } catch (JAXBException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

回答by Mickael

IMO, you should consider using tools to handle SOAP messages instead of doing it on your own.

IMO,您应该考虑使用工具来处理 SOAP 消息,而不是自己做

Examples :

例子 :



EDIT

编辑

There's a few things to say about your comment so I'll put my answer here.

关于您的评论,有几件事要说,所以我会在这里给出我的答案。

First,

第一的,

I have nothing to do with the API, all I do is make a POST request...

我与 API 无关,我所做的只是发出 POST 请求...

You have nothing to do with the APIbut you make a POST requestto the API. I think this is a figure of speech, right ?...

与 API 无关,但您向 API发出 POST 请求。我认为这是一个比喻,对吧?...

and there's no wsdl....

并且没有 wsdl ....

You can almost always get the WSDL of a SOAP webservice with this little trick. Just add ?wsdlat the end of the SOAP webservice URL.

您几乎总能通过这个小技巧获得 SOAP 网络服务的 WSDL。只需?wsdl在 SOAP 网络服务 URL 的末尾添加。

Example :

例子 :

Here's the URL of a SOAP webservice on the web (a real one) : http://www.webservicex.com/stockquote.asmx

这是网络上 SOAP 网络服务的 URL(真实的):http: //www.webservicex.com/stockquote.asmx

You can get its WSDL like this : http://www.webservicex.com/stockquote.asmx?wsdl

你可以像这样获得它的 WSDL:http: //www.webservicex.com/stockquote.asmx?wsdl

So the only option is to parse the response

所以唯一的选择是解析响应

IMO, there's almost always more than one solution to a problem in software development.

IMO,软件开发中的问题几乎总是有不止一种解决方案。