如何将SOAPMessage类型的对象转换为字符串?

时间:2016-03-08 15:08:08

标签: java web-services soap

我编写了以下代码来实现SAAJ。

SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();

// Send SOAP Message to SOAP Server
String url = "http://xxx.xxx.x.xx:8080/IssuerWebService";
soapResponse = soapConnection.call(createSOAPRequest(conf.TOKEN,conf.VERSION,
                                                     conf.CALLER_ID,conf.USER_ID,
                                                     conf.PASSWORD,strXML), url);

try{	   	        
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    soapResponse.writeTo(out);
    String response= out.toString();

但变量响应包含

com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl@7096faf1

createSOAPRequest()的代码如下所示

private static SOAPMessage createSOAPRequest() throws Exception {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();
        SOAPPart soapPart = soapMessage.getSOAPPart();

        String headerNameSpace = "https://paysecure/merchant.soap.header/";
        String bodyNameSpace = "https://paysecure/merchant.soap/";
        
        

        // SOAP Envelope
        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.addNamespaceDeclaration("mer", headerNameSpace);
        envelope.addNamespaceDeclaration("mer1", bodyNameSpace);

        /*
        Constructed SOAP Request Message:
        <?xml version="1.0" encoding="UTF-8"?>
			<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:mer="https://paysecure/merchant.soap.header/" xmlns:mer1="https://paysecure/merchant.soap/">
			   <soapenv:Header>
			      <RequestorCredentials xmlns="https://paysecure/merchant.soap.header/">
			         <Token>AF165FBD-4E85-4c66-BEB1-C54DC16CD48B</Token>
			         <Version>1.0.0.0</Version>
			         <CallerID>720203</CallerID>
			         <UserCredentials>
			            <UserID>test@acculynk.com</UserID>
			            <Password>Password!1</Password>
			         </UserCredentials>
			      </RequestorCredentials>
			   </soapenv:Header>
			   <soapenv:Body>
			      <CallPaySecure xmlns="https://paysecure/merchant.soap/">
			         <strCommand>Issuer_initiate</strCommand>
			         <strXML>&lt;paysecure&gt;&lt;partner_id&gt;ACCUTEST&lt;/partner_id&gt;&lt;card_no&gt;6073849900004960&lt;/card_no&gt;&lt;auth_method&gt;OTP&lt;/auth_method&gt;&lt;language_code&gt;en&lt;/language_code&gt;&lt;/paysecure&gt;</strXML>
			      </CallPaySecure>
			   </soapenv:Body>
			</soapenv:Envelope>
         */
        
        // Soap Header
        SOAPHeader soapHeader = envelope.getHeader();
        SOAPElement RequestorCredentials = soapHeader.addChildElement("RequestorCredentials", "mer");
        SOAPElement Token = RequestorCredentials.addChildElement("Token");
        Token.addTextNode("AF165FBD-4E85-4c66-BEB1-C54DC16CD48B");
        SOAPElement Version = RequestorCredentials.addChildElement("Version");
        Version.addTextNode("1.0.0.0");
        SOAPElement CallerID = RequestorCredentials.addChildElement("CallerID");
        CallerID.addTextNode("720203");
        SOAPElement UserCredentials = RequestorCredentials.addChildElement("UserCredentials");
        SOAPElement UserID = UserCredentials.addChildElement("UserID");
        UserID.addTextNode("test@acculynk.com");
        SOAPElement Password = UserCredentials.addChildElement("Password");
        Password.addTextNode("Password!1");
        
        // SOAP Body
        SOAPBody soapBody = envelope.getBody();
        SOAPElement CallPaySecure = soapBody.addChildElement("CallPaySecure", "mer1");
        SOAPElement strCommand = CallPaySecure.addChildElement("strCommand");
        strCommand.addTextNode("Issuer_Initiate");
        SOAPElement strXML = CallPaySecure.addChildElement("strXML");
        strXML.addTextNode("&lt;paysecure&gt;&lt;card_no&gt;60738499004978&lt;/card_no&gt; &lt;card_exp_date&gt;122014&lt;/card_exp_date&gt;&lt;card_holder_status&gt &lt;/card_holder_status&gt;&lt;cvd2&gt;0320&lt;/cvd2&gt;&lt;language_code&gt;en&lt;/language_code&gt; &lt;tran_id&gt;000000000000025236 &lt;/tran_id&gt;&lt;/paysecure&gt;");

        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("SOAPAction", headerNameSpace  + "CallPaySecure");

        soapMessage.saveChanges();       

        return soapMessage;
    }

如何获取响应中的元素?

0 个答案:

没有答案