2013-10-10 116 views
27

我對如何通過java向web服務發出請求感到困惑。使用java向WebService發出SOAP請求

現在我唯一明白的是webservices使用xml結構化的消息,但我仍然不完全明白如何構建我的請求。

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Body> 
    <getProductDetails xmlns="http://magazzino.example.com/ws"> 
     <productId>827635</productId> 
    </getProductDetails> 
    </soap:Body> 
</soap:Envelope> 

基本上我已經送2個參數的Web服務,作爲回報,我希望其他兩個參數。

我想有一些罐子可以完成大部分工作,但我沒有在網上找到任何東西。 有人可以解釋我的基礎嗎?

+1

如果您有wsdl文件,您可以生成java類並使用它們代替。 – RandomQuestion

+0

試着看[這個stackoverflow主題](http://stackoverflow.com/questions/19274828/how-to-use-wsdl/19276139#19276139)。我在那裏發佈一些對你有用的鏈接。 – Paolo

+1

是的,你正在談論的一些jar可以由你自己創建。查看wsimport的javdocs->從wsdl生成客戶端。如果你需要的話,你可以把它做成一個罐子。javadoc:http://docs.oracle.com/javase/7/docs/technotes/tools/share/wsimport.html,例如:http://www.mkyong.com/webservices/jax-ws/jax-ws- wsimport-tool-example/ –

回答

63

SOAP請求是一個XML文件,由您要發送到服務器的參數組成。

SOAP響應同樣也是一個XML文件,但現在該服務可以提供給您的所有內容。

基本上,WSDL是一個XML文件,它解釋了這兩個XML的結構。


使用Java實現簡單的SOAP客戶端,您可以使用SAAJ框架(它是隨JSE 1.6及以上):

帶附件的SOAP API的Java(SAAJ)是主要用於直接處理任何Web Service API中幕後發生的SOAP請求/響應消息。它允許開發人員直接發送和接收SOAP消息,而不是使用JAX-WS。

請參閱下面的使用SAAJ的SOAP Web服務調用的工作示例(運行它!)。它叫this web service

import javax.xml.soap.*; 

public class SOAPClientSAAJ { 

    // SAAJ - SOAP Client Testing 
    public static void main(String args[]) { 
     /* 
      The example below requests from the Web Service at: 
      http://www.webservicex.net/uszip.asmx?op=GetInfoByCity 


      To call other WS, change the parameters below, which are: 
      - the SOAP Endpoint URL (that is, where the service is responding from) 
      - the SOAP Action 

      Also change the contents of the method createSoapEnvelope() in this class. It constructs 
      the inner part of the SOAP envelope that is actually sent. 
     */ 
     String soapEndpointUrl = "http://www.webservicex.net/uszip.asmx"; 
     String soapAction = "http://www.webserviceX.NET/GetInfoByCity"; 

     callSoapWebService(soapEndpointUrl, soapAction); 
    } 

    private static void createSoapEnvelope(SOAPMessage soapMessage) throws SOAPException { 
     SOAPPart soapPart = soapMessage.getSOAPPart(); 

     String myNamespace = "myNamespace"; 
     String myNamespaceURI = "http://www.webserviceX.NET"; 

     // SOAP Envelope 
     SOAPEnvelope envelope = soapPart.getEnvelope(); 
     envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI); 

      /* 
      Constructed SOAP Request Message: 
      <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:myNamespace="http://www.webserviceX.NET"> 
       <SOAP-ENV:Header/> 
       <SOAP-ENV:Body> 
        <myNamespace:GetInfoByCity> 
         <myNamespace:USCity>New York</myNamespace:USCity> 
        </myNamespace:GetInfoByCity> 
       </SOAP-ENV:Body> 
      </SOAP-ENV:Envelope> 
      */ 

     // SOAP Body 
     SOAPBody soapBody = envelope.getBody(); 
     SOAPElement soapBodyElem = soapBody.addChildElement("GetInfoByCity", myNamespace); 
     SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("USCity", myNamespace); 
     soapBodyElem1.addTextNode("New York"); 
    } 

    private static void callSoapWebService(String soapEndpointUrl, String soapAction) { 
     try { 
      // Create SOAP Connection 
      SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); 
      SOAPConnection soapConnection = soapConnectionFactory.createConnection(); 

      // Send SOAP Message to SOAP Server 
      SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(soapAction), soapEndpointUrl); 

      // Print the SOAP Response 
      System.out.println("Response SOAP Message:"); 
      soapResponse.writeTo(System.out); 
      System.out.println(); 

      soapConnection.close(); 
     } catch (Exception e) { 
      System.err.println("\nError occurred while sending SOAP Request to Server!\nMake sure you have the correct endpoint URL and SOAPAction!\n"); 
      e.printStackTrace(); 
     } 
    } 

    private static SOAPMessage createSOAPRequest(String soapAction) throws Exception { 
     MessageFactory messageFactory = MessageFactory.newInstance(); 
     SOAPMessage soapMessage = messageFactory.createMessage(); 

     createSoapEnvelope(soapMessage); 

     MimeHeaders headers = soapMessage.getMimeHeaders(); 
     headers.addHeader("SOAPAction", soapAction); 

     soapMessage.saveChanges(); 

     /* Print the request message, just for debugging purposes */ 
     System.out.println("Request SOAP Message:"); 
     soapMessage.writeTo(System.out); 
     System.out.println("\n"); 

     return soapMessage; 
    } 

} 
+2

完美的例子。一個問題,你最喜歡閱讀XML響應輸出的方法是什麼?謝謝。 –

+1

我如何傳遞基本認證細節? – maamaa

+0

@maamaa如何http://stackoverflow.com/questions/17042259/java-saaj-basic-authentication? – acdcjunior

5

當WSDL可用時,只需執行兩個步驟即可調用該Web服務。

第1步:生成從WSDL2Java工具客戶端源

第2步:

YourService service = new YourServiceLocator(); 
Stub stub = service.getYourStub(); 
stub.operation(); 

如果你進一步看,你會發現,Stub類用於:通過調用操作調用在遠程位置部署的服務作爲Web服務。當調用它時,您的客戶端實際上會生成SOAP請求並進行通信。同樣,Web服務將響應作爲SOAP發送。藉助像Wireshark這樣的工具,您可以查看交換的SOAP消息。

但是,由於您已經要求瞭解基礎知識的更多解釋,所以我建議您參考here並使用它的客戶端編寫Web服務以進一步瞭解它。