2010-06-01 28 views
6

Vì vậy, tôi đang hết ý tưởng để cố gắng thực sự kết nối máy khách với dịch vụ SOAP mà tôi đang chạy qua trục2.chạy phiên bản máy khách trục2 1.5

Tôi đã thử hai phương pháp, một là sử dụng wsdl2java để xây dựng sơ khai và các lớp phía máy khách liên quan, sau đó viết một lớp Client để xây dựng các thông báo yêu cầu và gửi chúng thông qua Stub. Một cách khác là sử dụng các ServiceClient để kết nối ..

Cả hai đều thất bại theo cách riêng của họ ..

Lựa chọn # 1, mỗi khi một thông điệp được gửi qua cuống tôi nhận được trở lại này:

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) 

Lựa chọn # 2, mỗi khi chạy nó tôi nhận được ngoại lệ này:

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

Lựa chọn # 2 nguồn:

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

Bạn có gặp vấn đề với Tùy chọn số 1 không, tôi có cùng vấn đề. – metdos

Trả lời

3

Với sự báo trước rằng Axis2 là một buggy pile of crap, tôi gần đây đã phải viết một ứng dụng Axis2, và thấy rằng việc sử dụng các nhà xây dựng mặc định ServiceClient() không làm việc tốt - tôi đã tự tạo ra một ConfigurationContext, v.v. Tôi thấy rằng việc sử dụng ServiceClient.getOptions() thay vì tạo new Options() được lưu giữ một số dữ liệu mặc định. Tôi cũng khuyên bạn nên bỏ options.setTransportInProtocol(...) trừ khi bạn thực sự cần nó - mọi thứ sẽ hoạt động tốt qua HTTP mà không cần điều này. Ngoài ra, bạn có thể cần phải đặt options.setAction(...) để tương ứng với "hoạt động" trong WSDL của bạn.

Tôi đã bao gồm phần lớn ứng dụng khách của mình (với thông tin nhạy cảm bị loại bỏ), với hy vọng nó sẽ giúp ích. Bạn có thể bỏ qua một cách an toàn các phần liên quan đến việc giải quyết trừ khi bạn dự định sử dụng 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

+1 cho trục2 là một đống lỗi của crap – idursun

+0

Cũng hỗ trợ tuyên bố đó. Rõ ràng mọi người đã tìm thấy thành công với nó, nhưng cá nhân tôi chuyển sang Apache CXF ở giữa các vấn đề của tôi ở trên và không thấy lý do để nhìn lại. – Rich

5

Tôi cũng gặp lỗi "Luồng đầu vào cho thư đến rỗng" khi sử dụng Axis để kết nối với nhà cung cấp dịch vụ .Net.

Vấn đề ở chỗ .Net không hỗ trợ tính năng được gọi là "mã hóa chunked", theo mặc định, Axis sẽ phá vỡ tiêu đề yêu cầu của nó theo các phần được cho là tuân thủ HTTP 1.1.

Dù sao, bạn có thể tắt tính năng này trong Axis bằng cách làm như sau:

// 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);    

này đã làm việc cho tôi. Một điều khác để đảm bảo khi xử lý các dịch vụ .Net là để có thể chỉ định tên cổng và đảm bảo tải trọng thư của bạn có tiền tố không gian tên cho mỗi phần tử.

Hy vọng thông tin này sẽ giúp ai đó.

Chúc mừng, DC

1

Như đã nói bởi Danmar,

thử đoạn mã sau: giá trị thiết lập là true ...

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

Hy vọng nó hoạt động ...

Cảm ơn

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