2010-06-01 44 views
6

所以我想盡辦法讓客戶端連接到我通過axis2運行的SOAP服務。運行一個axis2客戶端版本1.5

我嘗試了兩種方法,一種是使用wsdl2java構建存根和關聯的客戶端類,然後編寫一個Client類來構建請求消息並通過存根發送它們。另一種方法是使用ServiceClient連接..

兩者都以自己的方式失敗..

選項#1,每天通過短信息發送時我得到這個回:

org.apache.axis2.AxisFault: The input stream for an incoming message is null. 
at org.apache.axis2.transport.TransportUtils.createSOAPMessage(TransportUtils.java:87) 
at org.apache.axis2.transport.TransportUtils.createSOAPMessage(TransportUtils.java:67) 
at org.apache.axis2.description.OutInAxisOperationClient.handleResponse(OutInAxisOperation.java:354) 
at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:417) 
at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:229) 
at org.apache.axis2.client.OperationClient.execute(OperationClient.java:165) 

選項#2,每次我運行它,我得到這個異常:

org.apache.axis2.deployment.DeploymentException: org.apache.axis2.transport.local.LocalTransportSender 

選項#2來源:

import javax.xml.stream.XMLStreamException; 
import org.apache.axiom.om.OMAbstractFactory; 
import org.apache.axiom.om.OMElement; 
import org.apache.axiom.om.OMFactory; 
import org.apache.axiom.om.OMNamespace; 
import org.apache.axis2.addressing.EndpointReference; 
import org.apache.axis2.client.Options; 
import org.apache.axis2.Constants; 
import org.apache.axis2.client.ServiceClient; 

public class loyaltyClient { 

    private static EndpointReference targetEPR = 
     new EndpointReference(
      "http://localhost:8080/axis2/services/service"); 

    public static OMElement verifyCustomer(String customer_id) { 
     OMFactory fac = OMAbstractFactory.getOMFactory(); 
     OMNamespace omNs = fac.createOMNamespace(
       "http://localhost/", "service"); 
     OMElement method = fac.createOMElement("VerifyCustomer", omNs); 
     OMElement value1 = fac.createOMElement("customer_id",omNs); 
     OMElement value2 = fac.createOMElement("source_id",omNs); 
     OMElement value3 = fac.createOMElement("source_password",omNs); 
     OMElement value4 = fac.createOMElement("source_txnid",omNs); 
     OMElement value5 = fac.createOMElement("timestamp",omNs); 

value1.addChild(fac.createOMText(value1, customer_id)); 
value2.addChild(fac.createOMText(value2, "source")); 
value3.addChild(fac.createOMText(value3, "1234")); 
value4.addChild(fac.createOMText(value4, "123")); 
value5.addChild(fac.createOMText(value5, "06-01-2010 12:01:01")); 
     method.addChild(value1); 
     method.addChild(value2); 
     method.addChild(value3); 
     method.addChild(value4); 
     method.addChild(value5); 
     return method; 
    } 

    public static void main(String[] args) { 
     try { 
      OMElement vctest = loyaltyClient.verifyCustomer("6177740603"); 
      Options options = new Options(); 
      options.setTo(targetEPR); 

options.setTransportInProtocol(Constants.TRANSPORT_HTTP); 

      ServiceClient sender = new ServiceClient(); 
      sender.setOptions(options); 
      OMElement result = sender.sendReceive(vctest); 

      String response = result.getFirstElement().getText(); 
      System.out.println(response); 

     } catch (Exception e) { //(XMLStreamException e) { 
      System.out.println(e.toString()); 
     } 
    } 

}

+0

你是否發現選項#1的問題,我有同樣的問題。 – metdos 2012-05-23 13:03:44

回答

3

隨着有條件的,就是Axis2是一種buggy pile of crap,最近我不得不寫一個Axis2客戶端,並且發現使用默認的構造函數ServiceClient()沒有工作 - 我不得不手動創建ConfigurationContext等我發現使用ServiceClient.getOptions()代替創建new Options()保留了一些默認數據。除非你真的需要,否則我建議你放棄options.setTransportInProtocol(...) - 沒有這個,所有的東西都可以通過HTTP正常工作。此外,您可能需要將options.setAction(...)設置爲與WSDL中的「操作」相對應。

我已經包含了我的客戶的大部分內容(剝離出敏感信息),希望它能起到幫助作用。除非您打算使用WS-Addressing,否則可以安全地忽略有關尋址的部分。

ConfigurationContext cfgCtx = null; 

try { 
    /* Passing null to both params causes an AxisConfiguration to be created that uses 
    * the default axis2.xml file, which is included in the axis2 distribution jar. 
    * This is ideal for our case, since we cannot pass a full file path (relative 
    * paths are not allowed) because we do not know where the customer will deploy 
    * the application. This also allows engaging modules from the classpath. */ 
    cfgCtx = ConfigurationContextFactory.createConfigurationContextFromFileSystem(null , null); 
} catch (AxisFault e) { 
    // Bubble up the error 
} 

ServiceClient svcClient = null; 
try { 
    svcClient = new ServiceClient(cfgCtx, null); 
} catch (AxisFault e) { 
    // Bubble up the error 
} 

try { 
    /* This will work with the above ConfigurationContext as long as the module 
    * (addressing-1.5.1.mar) is on the classpath, e.g. in shared/lib. */ 
    svcClient.engageModule("addressing"); 
} catch (AxisFault e) { 
    // Bubble up the error 
} 

Options opts = svcClient.getOptions(); 
opts.setTo(new EndpointReference("http://myservername:8080/axis2/services/MyService")); 
opts.setAction("urn:doSomething"); // Corresponds to the "operation" in MyService's WSDL 
opts.setSoapVersionURI(SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI); // Set output to SOAP 1.2 

SOAPFactory factory = OMAbstractFactory.getSOAP12Factory(); 
svcClient.addHeader(createSOAPSecurityHeader(factory, response)); // CreateSOAPHeader just creates an OMElement 

try { 
    svcClient.sendReceive(createSOAPBody(factory, response)); // CreateSOAPBody just creates an OMElement 
} catch (AxisFault e) { 
    throw new ResponseDeliveryException(1, "Error sending SOAP payload.", e); 
} 
+2

axis2的+1是一堆廢話 – idursun 2013-02-15 08:29:55

+0

也支持該聲明。很明顯,人們已經找到了成功,但我個人在上述問題中轉移到了Apache CXF上,並沒有看到回頭的理由。 – Rich 2013-03-29 22:25:42

5

我在使用Axis連接到.Net服務提供者時也遇到了錯誤「傳入消息的輸入流爲空」。

問題是.Net不支持稱爲「chunked encoding」的功能,默認情況下,Axis將以塊爲單位中斷它的請求頭,這個塊應該是符合HTTP 1.1標準的東西。

無論如何,你可以通過執行以下操作在軸關閉此功能:

// Turn off the Axsis Chunked feature, some service providers (like .Net) don't support chunked headers. 
Options options = serviceClient.getOptions(); 
options.setProperty(HTTPConstants.CHUNKED, Constants.VALUE_FALSE); 
serviceClient.setOptions(options);    

這爲我工作。處理.Net服務時要確保的另一件事是能夠指定端口名稱並確保您的消息負載具有每個元素的名稱空間前綴。

希望此信息有助於某人。

乾杯, DC

1

至於說通過Danmar,

試試下面的代碼:值設置爲true ...

Options options = serviceClient.getOptions(); 
options.setProperty(HTTPConstants.CHUNKED, Constants.VALUE_TRUE); 
serviceClient.setOptions(options); 

希望工程...

謝謝