简体   繁体   中英

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

I can also create a client snippet that uses axis2 to invoke the web service. 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).

What I don't know how to do is convert the byte[] or org.jdom.Document object to an OMElement object. Evidently the serviceClient.sendReceive(elem) takes an OMElement parameter.

Here is what I tried so far (I removed the OMElement that I sent out once I was convinced it was going out):

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. You only have to provide a wsdl file and it will generate client stubs. If you do not have an original wsdl, you can still make one yourself.

The best way for you is to create the wsdl file manually, generate the client stub and call the stub directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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