简体   繁体   English

如何使用axis2编写客户端以将序列化的xml对象发送到Web服务?

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

I'm having a conceptual problem preventing me from solving a trivial problem. 我遇到了一个概念性问题,无法解决一个琐碎的问题。 I need to send an object to a web service. 我需要将对象发送到Web服务。 I have an endpoint, and I have code that can serialize the object, so I can create an org.jdom.Document or a byte[] object containing the serialized object. 我有一个端点,并且我有可以序列化该对象的代码,因此我可以创建一个org.jdom.Document或一个包含序列化对象的byte []对象。

I can also create a client snippet that uses axis2 to invoke the web service. 我还可以创建一个使用axis2调用Web服务的客户端代码段。 Finally I have tried sending a manually created message to the web service (it has no WSDL ;( ) AND I have used Charles to see what is going out (the request). 最终,我尝试将手动创建的消息发送到Web服务(它没有WSDL;(),并且我使用Charles来查看发生了什么(请求)。

What I don't know how to do is convert the byte[] or org.jdom.Document object to an OMElement object. 我不知道该怎么做是将byte []或org.jdom.Document对象转换为OMElement对象。 Evidently the serviceClient.sendReceive(elem) takes an OMElement parameter. 显然,serviceClient.sendReceive(elem)采用OMElement参数。

Here is what I tried so far (I removed the OMElement that I sent out once I was convinced it was going out): 到目前为止,这是我尝试过的操作(在确信要退出时,我删除了我发送的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());
                              }

               }
}

The point of using axis2 is that it takes care of everything. 使用axis2的要点是它可以处理所有事情。 You only have to provide a wsdl file and it will generate client stubs. 您只需要提供一个wsdl文件,它将生成客户端存根。 If you do not have an original wsdl, you can still make one yourself. 如果没有原始的wsdl,您仍然可以自己制作。

The best way for you is to create the wsdl file manually, generate the client stub and call the stub directly. 最好的方法是手动创建wsdl文件,生成客户端存根,然后直接调用该存根。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM