简体   繁体   English

如何使用axis2从SOAP Web服务获取xml响应作为String

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

How to get xml response from SOAP webservice (I am using Axis2). 如何从SOAP webservice获取xml响应(我正在使用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. 我有一段时间没有使用Axis2,但这应该相当接近。

This code : 这段代码:

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

Returns a Axiom SoapEnvelope (Axiom is the internal XML processing engine of Axis2). 返回Axiom SoapEnvelope (Axiom是Axis2的内部XML处理引擎)。

Axiom is a pretty complex piece of code built with some optimization in mind. Axiom是一个非常复杂的代码片段,在构建时考虑了一些优化。 In particular 尤其是

  • it is capable of returning XML objects (just as SoapEnvelope ) without actually even parsing the XML tree. 它能够返回XML对象(就像SoapEnvelope ),甚至不用解析XML树。 This is a gain in performance and memory especially for complex SOAP messages (eg MTOM). 这是性能和内存的增益,特别是对于复杂的SOAP消息(例如MTOM)。
  • it is capable of parsing only certain parts of the XML message, and skipping others entirely 它只能解析XML消息的某些部分,并完全跳过其他部分
  • 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. 它能够以火灾和忘记的方式解析它(例如,读取每个XML标记,并在运行中报告它们,例如作为Stax事件),或者在解析它们时缓存它们,动态构建DOM表示,可以一遍又一遍地访问。

In this context, it is understandable that calling "toString" has some limitations. 在这种情况下,调用“toString”有一些限制是可以理解的。
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 ? ...). 应该注意的是,没有理由在soap信封上调用“toString”实际上应该返回序列化的SOAP消息(有或没有协议头,是加密的XML消息的加密或解密版本? ...)。
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 . 但是假设它确实返回了SOAP Envelope的XML消息,Axiom和Axis, 由于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. 如果您的Axiom解析器处于非缓存模式,则调用“toString”将以一次性方式将整个响应解析为文本,这意味着您以后无法再次访问它。 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. Axiom的SOAP Envelope实现了Axiom的可序列化类型,允许您在有或没有元素缓存的情况下控制序列化。

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 服务项目客户项目

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

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