簡體   English   中英

如何使用axis2編寫客戶端以將序列化的xml對象發送到Web服務?

[英]How Do I write a client using axis2 to send a serialized xml object to a web service?

我遇到了一個概念性問題,無法解決一個瑣碎的問題。 我需要將對象發送到Web服務。 我有一個端點,並且我有可以序列化該對象的代碼,因此我可以創建一個org.jdom.Document或一個包含序列化對象的byte []對象。

我還可以創建一個使用axis2調用Web服務的客戶端代碼段。 最終,我嘗試將手動創建的消息發送到Web服務(它沒有WSDL;(),並且我使用Charles來查看發生了什么(請求)。

我不知道該怎么做是將byte []或org.jdom.Document對象轉換為OMElement對象。 顯然,serviceClient.sendReceive(elem)采用OMElement參數。

到目前為止,這是我嘗試過的操作(在確信要退出時,我刪除了我發送的OMElement):

package testAxis2Client01;

import java.util.Map;

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.axiom.soap.SOAPEnvelope;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.transport.http.HTTPConstants;

public class testAxis2Client01 {

               private static final int MXMOCONNECTIONTIMEOUT = 2;//don't really know what this should be.
               /**
               * @param args
               */
               public static void main(String[] args) {
                              try {
                                             callAxisWS();
                              } catch (XMLStreamException e) {
                                             e.printStackTrace();
                              } catch (Exception e) {
                                             e.printStackTrace();
                              }

               }
public static void callAxisWS() throws XMLStreamException, Exception {

                              //Axis2 client code to call a WS

                              OMElement response=null;
                              try{
                                             OMFactory factory = OMAbstractFactory.getSOAP11Factory();
                                             SOAPEnvelope theEnvelope = OMAbstractFactory.getSOAP12Factory().getDefaultEnvelope();
                                             theEnvelope.declareNamespace("http://www.w3.org/2001/XMLSchema","xsd");
                                             theEnvelope.declareNamespace("http://www.w3.org/2001/XMLSchema-instance","xsi");

                                             ServiceClient serviceClient = new ServiceClient();
                                             Options options = serviceClient.getOptions();
                                             options.setProperty(HTTPConstants.AUTO_RELEASE_CONNECTION, true);               // Another API to release connection.
                                             options.setTimeOutInMilliSeconds(10000); // Setting the connection timeout.
                                             EndpointReference targetEPR = new EndpointReference(theUrl);
                                             options.setTo(targetEPR);
                                             options.setAction("processDocument");

                                             serviceClient.setOptions(options);
                                             //response = serviceClient.sendReceive(myOMElement);
                                             response = serviceClient.sendReceive(elem)
                                             if (response != null) {
                                                            System.out.println("SUCCESS!!");
                                                            System.out.println(response.toStringWithConsume());
                                             }
                              }catch(Exception af){
                                            af.printStackTrace();
                                            System.out.println(af.getMessage());
                              }

               }
}

使用axis2的要點是它可以處理所有事情。 您只需要提供一個wsdl文件,它將生成客戶端存根。 如果沒有原始的wsdl,您仍然可以自己制作。

最好的方法是手動創建wsdl文件,生成客戶端存根,然后直接調用該存根。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM