2016-06-09 76 views
0

我試圖在收到請求後發送SOAP消息。在Eclipse Mars 2中,我從.wsdl生成Java Bean Skeleton。 .wsdl只有一個名爲send的操作。在Axis2生成所有類和.xml文件後,它有一個名爲MyServiceSkeleton.java的類,其中send()方法是。Java Axis2發送SOAP響應

在該方法中,我能夠接收SOAP請求,並提取XML數據與此代碼:

String xml = myServiceClass.getOMElement(null, OMAbstractFactory.getOMFactory()).toStringWithConsume(); 

我現在想要一個新的SOAP消息,或者發送一個響應返回給請求者或給定的IP地址/網址。基本上我有一個XML字符串,我使用請求中的數據創建了一個XML字符串,並且我想將它轉換爲SOAP消息,然後將SOAP消息發送到某個地方。

我假設我必須向我要發送給它的服務器發出請求。我嘗試這樣做:

public void soapConnection() { 
    try { 
     SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); 
     SOAPConnection soapConnection = soapConnectionFactory.createConnection(); 

     // Send to server 
     String url = "some ip address or url"; 
     soapConnection.call(createSOAPRequest(), url); 
     soapConnection.close(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

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

    String uri = "some uri"; 
    SOAPEnvelope envelope = soapPart.getEnvelope(); 
    envelope.addNamespaceDeclaration("ns1", uri); 

    // Take XML String and turn into envelope 

    soapMessage.saveChanges(); 

    return soapMessage;  
} 

我不知道如何將部分soapMessage.saveChanges()代碼之前。任何幫助,將不勝感激。

回答

0

您可以通過更改服務方法返回類型(服務方法是暴露給正在使用您的服務的外部用戶的方法)向調用方發送一個字段或一個對象。欲瞭解更多詳情,請參閱解答此問題SOAP fault handling in java