2012-07-18 55 views
3

我正在嘗試使用以下SOAPClient for Java(詳細信息,我從以下教程http://users.skynet.be/pascalbotte/rcx-ws-doc/saajpost.htm獲得)。使用Java通用SOAPClient(SAAJ)時拋出異常?

但是,它似乎是拋出一個例外。

這裏是我的代碼:

javax.xml.soap.SOAPMessage msg; 
MessageFactory mf = MessageFactory.newInstance(); 
msg = mf.createMessage(); 
SOAPPart part = msg.getSOAPPart(); 
StreamSource source = new StreamSource(new File("samples/input1.xml」)); 
part.setContent(source); 
msg.saveChanges(); 

String endpoint = "http://ws1.parasoft.com/glue/calculator"; 

SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance(); 
SOAPConnection conn = scf.createConnection(); 
javax.xml.soap.SOAPMessage response = conn.call(msg, endpoint); 
TransformerFactory tff = TransformerFactory.newInstance(); 
Transformer tf = tff.newTransformer(); 
Source sc = response.getSOAPPart().getContent(); 
ByteArrayOutputStream ostream = new ByteArrayOutputStream(); 
StreamResult result = new StreamResult(ostream); 
tf.transform(sc, result); 
conn.close(); 

System.out.println(new String(ostream.toByteArray(), "UTF-8」)); 

在這個例子中,我們假設samples/input1.xml存在以下:

<s11:Envelope xmlns:s11="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s11:Body> 
     <ns1:add xmlns:ns1="http://www.parasoft.com/wsdl/calculator/"> 
     <ns1:x>248</ns1:x> 
     <ns1:y>365</ns1:y> 
     </ns1:add> 
    </s11:Body> 
</s11:Envelope> 

的樣品,我試圖用可以在這裏找到Web服務: http://www.service-repository.com/client/operations

當運行上述Java代碼(使用SOAPClient庫)時,引發以下異常:

Jul 19, 2012 3:50:11 AM com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection post 
SEVERE: SAAJ0008: Bad Response; cannot find /calculator 
Exception in thread "main" com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (404cannot find /calculator 
    at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:148) 
    at corebus.test.deprecated.TestMain.main(TestMain.java:1870) 
Caused by: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (404cannot find /calculator 
    at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:258) 
    at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:144) 
    ... 1 more 

CAUSE: 

com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (404cannot find /calculator 
    at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:258) 
    at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:144) 

使用這個網站,似乎服務已經啓動並運行,並且運行良好。

我甚至通過一個簡單的cURL請求驗證了服務端點是活動的,這令人驚訝地產生了正確的輸出。

curl -H "Content-Type: text/xml; charset=utf-8" -H "SOAPAction:http://www.parasoft.com/wsdl/calculator/" [email protected] http://ws1.parasoft.com/glue/calculator 

產生的輸出是:

<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/‘> 
    <soap:Body> 
     <n:addResponse xmlns:n='http://www.parasoft.com/wsdl/calculator/‘> 
      <n:Result xsi:type='xsd:float'>613.0</n:Result> 
     </n:addResponse> 
    </soap:Body> 
</soap:Envelope> 

所以,我的問題是:首先,什麼是錯的Java的代碼?它怎麼能被修復?或者,還有其他更好/更可靠的通用SOAPClient庫會被推薦嗎?

回答

0

通過在瀏覽器中將?wsdl附加到您的url中或以某種方式獲取他的WSDL來查看WSDL。檢查WSDl是否指定它是文檔樣式還是PRC樣式,並基於該樣式準備輸入xml。

如果可能的話,使用SOAP UI工具可以非常方便。

+0

我一直在尋找soapUI ..但是,如果我沒有弄錯,那麼需要與Java集成的依賴關係看起來非常龐大。你(或任何人)有使用soapUI作爲Java Lib的經驗嗎? – Larry 2012-07-18 18:59:34