【问题标题】:How to get request and response with Axis2?如何使用 Axis2 获取请求和响应?
【发布时间】:2013-11-06 17:03:41
【问题描述】:

我有一个使用 Axis2 生成的 SOAP 客户端。它使用 JAXB-RI,尽管这可能无关紧要。

我没有任何 XML 配置。只需获取使用wsdl2java 生成的Stub,准备响应(作为 POJO)并在Stub 上执行方法。

如何获取在此调用中交换的请求和响应的原始 XML(作为字符串)?

我发现service._getServiceClient().getLastOperationContext().getMessageContext("Out").getEnvelope()"In" 类似),但其中一个抛出异常,因为流已经被处理。

听起来很明显,但不知怎么做,我不知道怎么做,而且官方文档少得吓人。

【问题讨论】:

    标签: java soap axis2


    【解决方案1】:

    不确定“In”消息标签有什么问题,

    但在搜索时,发现以下 JIRA 票 https://issues.apache.org/jira/browse/AXIS2-5469 这指向 https://issues.apache.org/jira/browse/AXIS2-5202 并在讨论中找到了一个 WA 来解决这个问题 使用以下代码,我可以收听肥皂请求的响应消息。

     stub._getServiceClient().getAxisService().addMessageContextListener(
     new MessageContextListener() {
            public void attachServiceContextEvent(ServiceContext sc,
                MessageContext mc) {}
            public void attachEnvelopeEvent(MessageContext mc) {
                try
                { mc.getEnvelope().cloneOMElement().serialize(System.out); }
                catch (XMLStreamException e) {}
            }
      });
    

    【讨论】:

      【解决方案2】:

      我遇到了类似的问题,你可以参考下面的代码

      private void ResponseSOAPMessage(OperationContext opCtx, OMElement responseOM)
              throws AxisFault
            {
              MessageContext msgCtxIn = opCtx.getMessageContext("In");
              if (!msgCtxIn.getEnvelope().isComplete())
              {
                msgCtxIn.getEnvelope().getBody().getFirstOMChild().close(false);
                msgCtxIn.getEnvelope().getBody().addChild(responseOM);
                msgCtxIn.getEnvelope().getBody().getFirstOMChild().detach();
              }
              CommonsTransportHeaders inHeaders = (CommonsTransportHeaders)msgCtxIn.getProperty("TRANSPORT_HEADERS");
              if (msgCtxIn.getEnvelope().isComplete())
              {
                 System.out.println("SOAP Response:");
                 System.out.println(msgCtxIn.getEnvelope().toString());
              }
              else
              {
                 System.out.println("SOAP Response Message Body:");
                 System.out.println(responseOM.toString());
              }
            }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-26
        • 2011-02-01
        • 1970-01-01
        • 2015-10-17
        • 2016-09-02
        • 2017-11-08
        • 1970-01-01
        • 2016-04-10
        相关资源
        最近更新 更多