简体   繁体   中英

How to get xml response as String from SOAP webservice using axis2

How to get xml response from SOAP webservice (I am using Axis2). I have tried many things but nothing is working out for response. I tried below

stub._getServiceClient().getLastOperationContext().
      getMessageContext("In").getEnvelope().toString();

its giving exception:

Exception : java.lang.IllegalStateException:
Can't process next node because caching is disabled at
org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:176)

Please help in this.

I haven't used Axis2 in a while, but this should be fairly close.

This code :

stub._getServiceClient().getLastOperationContext().
            getMessageContext("In").getEnvelope()

Returns a Axiom SoapEnvelope (Axiom is the internal XML processing engine of Axis2).

Axiom is a pretty complex piece of code built with some optimization in mind. In particular

  • it is capable of returning XML objects (just as SoapEnvelope ) without actually even parsing the XML tree. This is a gain in performance and memory especially for complex SOAP messages (eg MTOM).
  • it is capable of parsing only certain parts of the XML message, and skipping others entirely
  • it is capable of parsing it in a fire and forget manner (eg read each XML tag, and report them on the fly for example as Stax Events), or to cache them as it parses them, building a DOM representation on the fly, that can be accessed over and over again.

In this context, it is understandable that calling "toString" has some limitations.
One should remark that there is no reason that calling "toString" on a soap envelope should actually return the serialized SOAP Message at all (would that be with or without protocol headers, would that be the encrypted or decrypted version of an encrypted XML message ? ...).
But assuming it does return the SOAP Envelope's XML message, Axiom, and Axis, have to be carefull about it, because of the caching ability of Axiom .

If your Axiom parser is in non caching mode, calling "toString" will parse the whole response as a text in a one-shot manner, meaning that you could not ever access it again later. Which is a dangerous behavior. Therefore, by default, it is forbidden, and it fails with your exception.

But all is not lost. Axiom's SOAP Envelope implements Axiom's serializable types, which allow you to control serialization with or without element caching.

For example the serialize method

You could therefore use the following code (adapt it to your particular case) :

SOAPEnvelope env = ... // your enveloppe
boolean withCaching = true; // If you want you env object to remain useable later, false if you do not care and want to save memory
StringWriter xmlMessageWriter = new StringWriter();
XMLStreamWriter serializationTarget = XMLOutputFactory.newFactory().createXMLStreamWriter(xmlMessageWriter);
env.serialize(serializationTarget, withCaching);
String soapEnvelopeAsString = xmlMessageWriter.toString();

You can get idea following this

Service Project and Client Project

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