gpt4 book ai didi

java - 从 SOAP xml 解码,getSOAPBody() 方法返回 Null

转载 作者:行者123 更新时间:2023-12-02 10:47:31 25 4
gpt4 key购买 nike

解码 Soap 响应时 getSOAPBody 返回 null。 我尝试过使用 XMLStreamReader 在这种情况下有效。但我想知道一种通过 ByteArrayInputStream 做到这一点的方法。 正确的解决方法是什么?我附上下面的 POJOmand package-info.java 文件。

此外,如果我将“xsi:type =”xsd:string”设置为“xsi:type =”string”,它似乎可以工作。我应该如何修改我的 bean 类才能使其正常工作?

package-info.java

@XmlSchema(

namespace = "http://tempuri.org/",

xmlns = {

@javax.xml.bind.annotation.XmlNs(prefix = "soap", namespaceURI = "http://schemas.xmlsoap.org/soap/envelope/"),

@javax.xml.bind.annotation.XmlNs(prefix = "xsi", namespaceURI = "http://www.w3.org/2001/XMLSchema-instance/"),

@javax.xml.bind.annotation.XmlNs(prefix = "xsd", namespaceURI = "http://www.w3.org/2001/XMLSchema")

})
package org.tempuri;

import javax.xml.bind.annotation.XmlSchema;

TestJAX.java

    package org.test;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.stream.XMLStreamException;

import org.tempuri.WIResponse;

/**
* Test Class
*
* @author vijay.prakash
*
*/
public class TestJAX {
public static void main(String[] args) throws XMLStreamException, JAXBException, IOException, SOAPException {
// xmlReadingWay
/*
* XMLInputFactory xif = XMLInputFactory.newFactory(); XMLStreamReader xsr =
* xif.createXMLStreamReader(new FileReader("D:\\BACKUPWARS\\some.xml"));
*
* xsr.nextTag(); // Advance to Envelope tag xsr.nextTag(); // Advance to Body
* tag xsr.nextTag(); // Advance to getNumberResponconse tag
*/

// byteway
String object = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n"
+ "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"\r\n"
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\r\n"
+ " <soap:Body>\r\n" + " <WIResponse xmlns=\"http://tempuri.org/\">\r\n"
+ " <WIResult xsi:type=\"xsd:string\">20702458ghhc</WIResult>\r\n"
+ " </WIResponse>\r\n" + " </soap:Body>\r\n" + "</soap:Envelope>";

String str = new String(object.getBytes(), "utf-8");
final MessageFactory messFac = MessageFactory.newInstance();
SOAPMessage message = messFac.createMessage(null, new ByteArrayInputStream(str.getBytes()));

message.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "utf-8");
System.out.println("message: " + message.getSOAPBody());

JAXBContext jaxbContext = JAXBContext.newInstance(WIResponse.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

System.out.println("message.getSOAPBody(): " + message.getSOAPBody());
Object je = unmarshaller.unmarshal(message.getSOAPBody().extractContentAsDocument());

WIResponse wi = (WIResponse) je;

/*
* //JAXBElement<WIResponse> je =
* unmarshaller.unmarshal(message.getSOAPBody().extractContentAsDocument(),
* WIResponse.class);
*/
System.out.println(wi.getWIResult());

}
}

soap xml

    <?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<WIResponse xmlns="http://tempuri.org/">
<WIResult xsi:type="xsd:string">20702458ghhc</WIResult>
</WIResponse>
</soap:Body>
</soap:Envelope>

WIResponse.java

    package org.tempuri;

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

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "WIResponse", namespace = "http://tempuri.org/")
public class WIResponse {
@XmlElement(name = "WIResult", required = true, type = java.lang.String.class)
String wIResult;

public String getWIResult() {
return wIResult;
}

public void setWIResult(String wIResult) {
this.wIResult = wIResult;
}


}

最佳答案

问题似乎是某种解析错误(getSOAPBody() 似乎无法正确处理 namespace 解析)。

解决这个问题的“最简单”方法是自己从 SOAP 消息中检索原始 XML,但是在典型的 Java 方式中,该解决方案是复杂且官僚的。

您可能需要修改下面的示例代码:

//https://docs.oracle.com/javase/7/docs/api/javax/xml/soap/SOAPPart.html
javax.xml.soap.SOAPPart GSP = SomeSOAPMessage.getSOAPPart();

//https://static.javadoc.io/com.sun.xml.messaging.saaj/saaj-impl/1.4.0/com/sun/xml/messaging/saaj/util/JAXMStreamSource.html
com.sun.xml.messaging.saaj.util.JAXMStreamSource GC = GSP.getContent();

//https://static.javadoc.io/com.sun.xml.messaging.saaj/saaj-impl/1.4.0/com/sun/xml/messaging/saaj/util/ByteInputStream.html
com.sun.xml.messaging.saaj.util.ByteInputStream BIS = GC.getInputStream();

byte[] ByteArray = BIS.getBytes();

java.lang.String Data = new java.lang.String(ByteArray, "UTF-8");

基本上,从消息中的 SOAPPart 获取该 SOAPPart 的内容,它是一个 JAXMStreamSource。

然后,从 JAXMStreamSource 中,它可以有一个“Reader”或一个“InputStream”(在我的例子中,我发现它有一个“InputStream”)。 InputStream 可能由应用程序类型决定(在我的例子中,xop+xml,它是“二进制”XML),但是在测试中我发现它是 ByteInputStream 类型,它是 ByteArrayInputStream 的扩展类。

最后,您可以将 ByteArray 转换为字符串。该字符串将包含整个 XML(包括 XML 版本 header ),然后您可以使用您想要使用的任何首选 XML 处理程序类对其进行解析。

(包含全名解析和带注释的引用文献,以帮助理解其工作原理。)

关于java - 从 SOAP xml 解码,getSOAPBody() 方法返回 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52443026/

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