我正在尝试使用java中的soap
发出SAAJ
请求。我正在尝试制作以下xml:
必需的XML
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:paygo="http://paygo24.com/v3/protocol"
xmlns:tem="http://tempuri.org/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<tem:SendRequest>
<paygo:request xsi:type="paygo:RegisterCheckRequest" Service="42" Id="7a646d45-ee2f-4b1c-8de8-780c416fbbd0">
<PaymentParameters>
<Parameter Name="account" Value="08374829"/>
</PaymentParameters>
</paygo:request>
<tem:pointId>46</tem:pointId>
<tem:password>alkdsjflasdkjfl+ak87+UI90==</tem:password>
</tem:SendRequest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
JAVA SAAJ
package ussd.utils;
import javax.xml.namespace.QName;
import javax.xml.soap.*;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
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://paygo24.com/v3/protocol";
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
// print SOAP Response
System.out.print("Response SOAP Message:");
createSoapResponse(soapResponse);
// soapResponse.writeTo(System.out);
soapConnection.close();
}
private static void createSoapResponse(SOAPMessage soapResponse) throws Exception {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
Source sourceContent = soapResponse.getSOAPPart().getContent();
System.out.println("\n----------SOAP Response-----------");
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
}
private static SOAPMessage createSOAPRequest() throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.removeNamespaceDeclaration(envelope.getPrefix());
envelope.addNamespaceDeclaration("SOAP-ENV","http://schemas.xmlsoap.org/soap/envelope/");
envelope.setPrefix("SOAP-ENV");
envelope.addNamespaceDeclaration("paygo","http://paygo24.com/v3/protocol");
envelope.addNamespaceDeclaration("tem","http://tempuri.org/");
envelope.addNamespaceDeclaration("xsd","http://www.w3.org/2001/XMLSchema");
envelope.addNamespaceDeclaration("xsi","http://www.w3.org/2001/XMLSchema-instance");
SOAPHeader header=soapMessage.getSOAPHeader();
header.setPrefix("SOAP-ENV");
SOAPBody soapBody = envelope.getBody();
soapBody.setPrefix("SOAP-ENV");
SOAPElement root=soapBody.addChildElement(new QName("tem:SendRequest"));
SOAPElement request=root.addChildElement(new QName("paygo:request"));
//SOAPElement root=soapBody.addChildElement(new QName("","tem:SendRequest"));
//SOAPElement request=root.addChildElement(new QName("","paygo:request"));
request.setAttribute("xsi:type", "paygo:RegisterCheckRequest");
request.setAttribute("Id","7a646d45-ee2f-4b1c-8de8-780c416fbbd0");
request.setAttribute("Service","42");
SOAPElement paymentParameters =request.addChildElement(new QName(" ","PaymentParameters"));
SOAPElement parameter=paymentParameters.addChildElement("Parameter");
paymentParameters.setAttribute("xmlns","");
parameter.setAttribute("Name","account");
parameter.setAttribute("Value", "08374829");
root.addChildElement("tem:pointId").setValue("46");
root.addChildElement("tem:password").setValue("alkdsjflasdkjfl+ak87+UI90==");
soapMessage.getSOAPHeader().detachNode();
soapMessage.saveChanges();
soapMessage.writeTo(System.out);
return soapMessage;
}
}
但我在SOAPElement request=root.addChildElement(new QName("paygo:request"));
行跟踪异常。
的异常
Exception in thread "main" org.w3c.dom.DOMException: NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.
at com.sun.org.apache.xerces.internal.dom.ElementNSImpl.setName(Unknown Source)
at com.sun.org.apache.xerces.internal.dom.ElementNSImpl.<init>(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.impl.ElementImpl.<init>(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.impl.ElementFactory.createElement(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl.createElement(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.impl.ElementImpl.createElement(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.impl.ElementImpl.addElement(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.impl.ElementImpl.addChildElement(Unknown Source)
at ussd.utils.SOAPClientSAAJ.createSOAPRequest(SOAPClientSAAJ.java:58)
at ussd.utils.SOAPClientSAAJ.main(SOAPClientSAAJ.java:18)
答案 0 :(得分:1)
尝试使用此SOAP信封创建代码,它应该创建所需的格式。
private static SOAPMessage createSOAPRequest() throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.removeNamespaceDeclaration(envelope.getPrefix());
envelope.addNamespaceDeclaration("SOAP-ENV","http://schemas.xmlsoap.org/soap/envelope/");
envelope.setPrefix("SOAP-ENV");
envelope.addNamespaceDeclaration("paygo","http://paygo24.com/v3/protocol");
envelope.addNamespaceDeclaration("tem","http://tempuri.org/");
envelope.addNamespaceDeclaration("xsd","http://www.w3.org/2001/XMLSchema");
envelope.addNamespaceDeclaration("xsi","http://www.w3.org/2001/XMLSchema-instance");
SOAPHeader header=soapMessage.getSOAPHeader();
header.setPrefix("SOAP-ENV");
SOAPBody soapBody = envelope.getBody();
soapBody.setPrefix("SOAP-ENV");
SOAPElement root=soapBody.addChildElement("SendRequest","tem");
SOAPElement request=root.addChildElement("request","paygo");
request.setAttribute("xsi:type", "paygo:RegisterCheckRequest");
request.setAttribute("Id","7a646d45-ee2f-4b1c-8de8-780c416fbbd0");
request.setAttribute("Service","42");
SOAPElement paymentParameters =request.addChildElement("PaymentParameters");
SOAPElement parameter=paymentParameters.addChildElement("Parameter");
paymentParameters.setAttribute("xmlns","");
parameter.setAttribute("Name","account");
parameter.setAttribute("Value", "08374829");
root.addChildElement("pointId","tem").setValue("46");
root.addChildElement("password","tem").setValue("alkdsjflasdkjfl+ak87+UI90==");
soapMessage.getSOAPHeader().detachNode();
soapMessage.saveChanges();
soapMessage.writeTo(System.out);
return soapMessage;
}