2013-10-10 27 views
27

Tôi hơi bối rối về cách thực hiện yêu cầu tới dịch vụ web qua java.Yêu cầu SOAP tới WebService với java

Hiện tại, điều duy nhất tôi hiểu là các dịch vụ web sử dụng các tin nhắn có cấu trúc xml, nhưng tôi vẫn chưa hiểu cách cấu trúc yêu cầu của mình.

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

Về cơ bản tôi gửi 2 thông số cho dịch vụ web và ngược lại tôi mong đợi hai thông số khác.

Tôi đoán có một số lọ có thể thực hiện hầu hết công việc, nhưng tôi không tìm thấy bất kỳ trực tuyến nào. Ai đó có thể giải thích cho tôi cơ sở không?

+1

nếu bạn có tệp wsdl, bạn có thể tạo các lớp java và sử dụng chúng thay thế. – RandomQuestion

+0

Hãy thử xem [chủ đề ngăn xếp luồng này] (http://stackoverflow.com/questions/19274828/how-to-use-wsdl/19276139#19276139). Tôi đăng ở đó một số liên kết có thể hữu ích cho bạn. – Paolo

+1

có ,, một số lọ bạn đang nói về có thể được tạo ra bởi chính mình. nhìn vào javdocs cho wsimport-> tạo ra các máy khách từ wsdl. bạn có thể làm cho nó một cái bình nếu bạn cần.javadoc: http: //docs.oracle.com/javase/7/docs/technotes/tools/share/wsimport.html, ví dụ: http: //www.mkyong.com/webservices/jax-ws/jax-ws- wsimport-tool-example/ –

Trả lời

63

Yêu cầu SOAP là tệp XML bao gồm các thông số bạn đang gửi đến máy chủ.

Phản hồi SOAP giống như một tệp XML, nhưng bây giờ với mọi thứ dịch vụ muốn cung cấp cho bạn.

Về cơ bản WSDL là một tệp XML giải thích cấu trúc của hai XML đó.


Thực hiện khách hàng SOAP đơn giản trong Java, bạn có thể sử dụng khuôn khổ SAAJ (nó được vận chuyển với JSE 1.6 trở lên):

SOAP với Attachments API cho Java (SAAJ) là chủ yếu được sử dụng để xử lý trực tiếp với các thông báo Yêu cầu/Trả lời SOAP xảy ra đằng sau hậu trường trong bất kỳ API dịch vụ web nào. Nó cho phép các nhà phát triển trực tiếp gửi và nhận tin nhắn xà phòng thay vì sử dụng JAX-WS.

Xem bên dưới ví dụ hoạt động (chạy nó!) Của cuộc gọi dịch vụ web SOAP bằng SAAJ. Nó gọi 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

Ví dụ hoàn hảo. Một câu hỏi, phương pháp ưa thích của bạn đọc đầu ra phản ứng xml là gì? Cảm ơn. –

+1

làm cách nào tôi có thể vượt qua chi tiết xác thực cơ bản? – maamaa

+0

@maamaa Làm thế nào về http://stackoverflow.com/questions/17042259/java-saaj-basic-authentication? – acdcjunior

5

Khi WSDL khả dụng, bạn chỉ cần thực hiện hai bước để thực hiện dịch vụ web đó.

Bước 1: Tạo nguồn phía khách hàng từ một công cụ WSDL2Java

Bước 2: Gọi các hoạt động sử dụng:

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

Nếu bạn nhìn xa hơn, bạn sẽ nhận thấy rằng lớp Stub được sử dụng để gọi dịch vụ được triển khai tại vị trí từ xa dưới dạng dịch vụ web. Khi gọi điều đó, máy khách của bạn thực sự tạo ra yêu cầu SOAP và giao tiếp. Tương tự, dịch vụ web gửi phản hồi như là một SOAP. Với sự trợ giúp của một công cụ như Wireshark, bạn có thể xem các thông báo SOAP được trao đổi.

Tuy nhiên vì bạn đã yêu cầu giải thích thêm về các khái niệm cơ bản, tôi khuyên bạn nên tham khảo here và viết dịch vụ web với ứng dụng khách để tìm hiểu thêm.

Các vấn đề liên quan