2014-11-03 43 views
1

我提供了一個java代碼,我在這裏找到了堆棧溢出的地方,我想用我的目的類似的東西。此代碼可以正常工作,但是我面臨的問題仍然存在。檢查我在代碼中完成的系統操作。前兩個系統輸出,我打印得到響應的SOAP正文返回給我一個null,而當我嘗試打印整個響應它打印準確。它非常奇怪。getSOAPBody返回NULL,而SOAPResponse.writeTo打印整個消息,奇怪?

有誰知道爲什麼發生這種情況,以及如何解決它。我使用JDK 1.7

package trials; 

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; 


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); 


    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>[email protected]</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("[email protected]"); 
    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; 
} 

}

這是實現爲["+getNodeName()+": "+getNodeValue()+"]輸出,我得到的控制檯

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>[email protected]</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>Mail Server will accept email</ResponseText><ResponseCode>3</ResponseCode><LastMailServer>gmail-smtp-in.l.google.com</LastMailServer><GoodEmail>true</GoodEmail></VerifyEmailResult></VerifyEmailResponse></soap:Body></soap:Envelope> 

回答

1

toString()Node,並且getNodeValue()將爲空元素節點。因此,您將獲得[soap:Body: null]

但是,身體在那裏,你可以像平常一樣使用它。

請嘗試以下方法看到,所有的元素都在那裏:

SOAPBody body = soapResponse.getSOAPBody(); 
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()); 
+0

謝謝。但後來我試圖將它解組到一個java類來獲得作爲java對象的SOAP主體。這沒有工作,因爲我猜這個NULL的問題。任何指針? – ydb 2014-11-03 16:19:05

+0

由於身體存在,所以'null'不太可能是原因。也許你應該用你的編組代碼和問題描述來創建一個新的問題。 – Khalid 2014-11-03 16:57:08

+0

這裏是我與解組代碼的新職位 - https://stackoverflow.com/questions/26719586/cannot-unmarshal-soap-body-to-java-object – ydb 2014-11-03 17:28:26

2

我遵循同樣的教程爲你做。

,並面臨着同樣的問題,只是如果別人需要這個,我通過刪除名字空間別名「示例」解決了這個問題,在內部節點,我只把它放在第一個參數節點search

SOAPBody soapBody = envelope.getBody(); 
    SOAPElement soapBodyElem = soapBody.addChildElement("search", "example"); /*"example"*/ 

SOAPElement soapBodyElem0 = soapBodyElem.addChildElement("arg0"); 
SOAPElement soapBodyElem1 = soapBodyElem0.addChildElement("name"); 
soapBodyElem1.addTextNode("test"); 
SOAPElement soapBodyElem2 = soapBodyElem0.addChildElement("number"); 
soapBodyElem2.addTextNode("5"); 

MimeHeaders headers = soapMessage.getMimeHeaders(); 
headers.addHeader("SOAPAction", serverURI + "search"); 

正如我在soapUI的自動生成的XML(僅在search節點)看到

<example:search> 
     <!--Optional:--> 
     <arg0> 
      <!--Optional:--> 
      <name>234</name> 
      <number>5</number> 
     </arg0> 
     </example:search> 

這解決了我的問題。