这与我之前的帖子getSOAPBody returns NULL whereas SOAPResponse.writeTo prints the whole message, Strange?
有关我发布了我用于解组的代码。我收到目标对象中的所有空值
package trials;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Unmarshaller;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.stream.StreamSource;
public class SOAPClientSAAJ {
public static void main(String args[]) throws Exception {
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Send SOAP Message to SOAP Server
String url = "http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx";
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
System.out.println("Body");
// print SOAP Response
System.out.print("Response SOAP Message:");
System.out.println("SOAP Body 2= " + soapResponse.getSOAPBody());
System.out.println("SOAP Body 2=" + soapResponse.getSOAPPart().getEnvelope().getBody());
soapResponse.writeTo(System.out);
SOAPBody body = soapResponse.getSOAPBody();
System.out.println("\n");
System.out.println(body.getElementsByTagName("ResponseText").item(0).getTextContent());
System.out.println(body.getElementsByTagName("ResponseCode").item(0).getTextContent());
System.out.println(body.getElementsByTagName("GoodEmail").item(0).getTextContent());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
soapResponse.writeTo(bos);
XMLInputFactory xif = XMLInputFactory.newFactory();
StreamSource xml = new StreamSource(new ByteArrayInputStream(bos.toByteArray()));
XMLStreamReader xsr = xif.createXMLStreamReader(xml);
xsr.nextTag();
while (!xsr.getLocalName().equals("VerifyEmailResult")) {
xsr.nextTag();
}
JAXBContext jc = JAXBContext.newInstance(VerifyEmailResult.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBElement<VerifyEmailResult> jb = unmarshaller.unmarshal(xsr, VerifyEmailResult.class);
xsr.close();
VerifyEmailResult v = jb.getValue();
System.out.println(v.ResponseText);
System.out.println(v.ResponseCode);
System.out.println(v.LastMailServer);
System.out.println(v.GoodEmail);
soapConnection.close();
}
private static SOAPMessage createSOAPRequest() throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
String serverURI = "http://ws.cdyne.com/";
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("example", serverURI);
/*
* Constructed SOAP Request Message: <SOAP-ENV:Envelope
* xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
* xmlns:example="http://ws.cdyne.com/"> <SOAP-ENV:Header/>
* <SOAP-ENV:Body> <example:VerifyEmail>
* <example:email>mutantninja@gmail.com</example:email>
* <example:LicenseKey>123</example:LicenseKey> </example:VerifyEmail>
* </SOAP-ENV:Body> </SOAP-ENV:Envelope>
*/
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("VerifyEmail", "example");
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("email", "example");
soapBodyElem1.addTextNode("mutantninja@gmail.com");
SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("LicenseKey", "example");
soapBodyElem2.addTextNode("123");
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", serverURI + "VerifyEmail");
soapMessage.saveChanges();
/* Print the request message */
System.out.print("Request SOAP Message:");
soapMessage.writeTo(System.out);
System.out.println();
return soapMessage;
}
}
这是我的班级,我试图解散
package trials;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class VerifyEmailResult {
public String ResponseText;
public String ResponseCode;
public String LastMailServer;
public String GoodEmail;
}
这是我的控制台输出
Request SOAP Message:<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:example="http://ws.cdyne.com/"><SOAP-ENV:Header/><SOAP-ENV:Body><example:VerifyEmail><example:email>mutantninja@gmail.com</example:email><example:LicenseKey>123</example:LicenseKey></example:VerifyEmail></SOAP-ENV:Body></SOAP-ENV:Envelope>
Body
Response SOAP Message:SOAP Body 2= [soap:Body: null]
SOAP Body 2=[soap:Body: null]
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soap:Body><VerifyEmailResponse xmlns="http://ws.cdyne.com/"><VerifyEmailResult><ResponseText>Current license key only allows so many checks</ResponseText><ResponseCode>9</ResponseCode><LastMailServer/><GoodEmail>false</GoodEmail></VerifyEmailResult></VerifyEmailResponse></soap:Body></soap:Envelope>
Current license key only allows so many checks
9
false
null
null
null
null
答案 0 :(得分:1)
尝试
response.response.getSOAPBody().extractContentAsDocument()
我遇到了同样的问题,并通过以下代码修复:
SOAPMessage response = dispatch.invoke(request);
Document document = response.getSOAPBody().extractContentAsDocument();
NodeList list = document.getChildNodes();
System.out.println("Value : " + list.item(0).getChildNodes().item(0).getChildNodes().item(0).getTextContent());
希望这有帮助