gpt4 book ai didi

java - 通过 JAXB 解析 Soap 响应 header 元素

转载 作者:太空宇宙 更新时间:2023-11-04 12:03:59 25 4
gpt4 key购买 nike

我有以下 SOAP 响应 header ,我想使用 JAXB 从中解码元素的值。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:com="http://example.com" xmlns:ass="http://example.com">
<soapenv:Header>
<com:HeaderResponse>
<com:RequestId>1234</com:RequestId>
</com:HeaderResponse>
</soapenv:Header>
<soapenv:Body>
......................
</soapenv:Body>
</soapenv:Envelope>

我尝试了很多选项,但无法获取 RequestID 元素的值。其中一种方法是:

package com.example.model;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "HeaderResponse", propOrder = {
"RequestId"
})
@XmlRootElement(name = "HeaderResponse")
public class Header {
@XmlElement(name = "RequestId",required = true)
protected String RequestId;

public String getRequestId() {
return RequestId;
}

public void setRequestId(String RequestId) {
this.RequestId = RequestId;
}
}

调用代码

JAXBContext jaxbContext = JAXBContext.newInstance(Header.class);
SOAPMessage requestMessage = MessageFactory.newInstance().createMessage(null,
new ByteArrayInputStream(file.getBytes()));;
SOAPHeader reqheader = requestMessage.getSOAPHeader();
Iterator<SOAPHeaderElement> itr = reqheader.examineAllHeaderElements();
while (itr.hasNext()) {
SOAPHeaderElement ele = itr.next();

Header headerType=(Header)jaxbContext.createUnmarshaller().unmarshal(ele);
System.out.println(headerType.getRequestId());
}

我得到的异常:

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://example.com", local:"HeaderResponse"). Expected elements are <{}HeaderResponse>
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(Unknown Source)
at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(Unknown Source)
at com.sun.xml.internal.bind.unmarshaller.DOMScanner.scan(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source)
at com.example.model.XYZ.parseAndPrintXMLTagValues(XYZ.java:69)
at com.example.main.RunJaxB.main(RunJaxB.java:18)

最佳答案

不要编写自己的代码。只需获取 XSD 文件并使用 XSD 文件生成 java 类,然后您将能够获得正确的数据:供引用: https://dzone.com/articles/using-jaxb-generate-java

关于java - 通过 JAXB 解析 Soap 响应 header 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40607257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com