简体   繁体   中英

How to get request and response with Axis2?

I have a SOAP client generated with Axis2. It uses JAXB-RI, though that probably does not matter.

I do not have any XML configuration. Just get the Stub generated with wsdl2java , prepare the response (as POJO) and execute method on the Stub .

How can I get the raw XML (as String) for request and response that are exchanged on this call?

I figured out service._getServiceClient().getLastOperationContext().getMessageContext("Out").getEnvelope() (and similar for "In" ), but one of them throws an exception because the stream has already been processed.

It sounds very obvious, yet somehow I cannot figure out how to do it, and the official documentation is intimidatingly scarce.

Not sure what is issue with "In" Message Lable,

But while searching, found following JIRA ticket https://issues.apache.org/jira/browse/AXIS2-5469 which points to https://issues.apache.org/jira/browse/AXIS2-5202 And in discussion found one of the WA to solve this issue using following code, I am able to listen Response message for the soap Request.

 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) {}
        }
  });

i was facing similar issue , you can take reference from below code

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());
        }
      }

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