简体   繁体   中英

Print Axis2 Request Response XML

I want to print RAW Request Response XML to console. I have created the stubs using wsdl2java axis2.

wsdl2java has created 2 java files, one for stub and one for callbackhandler.

I am trying with below method but getting null value for

operationContext.getMessageContext("Out");
/
operationContext.getMessageContext("In");.

Code

public void SOAPLogHandler(Stub stub){  

ServiceContext serviceConxt = stub._getServiceClient().getServiceContext();

//**** Enable the Cache to hold the last operation
OperationContext OperationContext  = new OperationContext();
boolean cacheLastOperationContext = true;
OperationContext.setComplete(true); // Enable the Cache value
serviceConxt.setCachingOperationContext(cacheLastOperationContext);
serviceConxt.setLastOperationContext(OperationContext);

OperationContext operationContext = serviceConxt.getLastOperationContext();

if (operationContext != null) {
         MessageContext outMessageContext = operationContext.getMessageContext("Out");
             operationContext.getMessageContexts();
        if (outMessageContext != null) {
            System.out.println("OUT SOAP: "+outMessageContext.getEnvelope().toString());
             }
     MessageContext inMessageContext = operationContext.getMessageContext("In");
if (inMessageContext != null) {
    System.out.println("IN SOAP: "+ inMessageContext.getEnvelope().toString());
                  }
           }

Can you please let me know is there any other way to get the raw xml

I managed to do so by adding a couple of lines in a method of the stub (the one you call to invoke the web service). I identified the line, ie

_operationClient.execute(true);

that effectively contacts the web service and added just after it:

_operationClient.getMessageContext("Out").getEnvelope().serialize(System.out);
_operationClient.getMessageContext("In").getEnvelope().serialize(System.out);

不幸的是,除非你通过说tcp monitor拦截消息,否则你将很难获得原始消息。

In the above code:

  OperationContext OperationContext  = new OperationContext();
  boolean cacheLastOperationContext = true;
  OperationContext.setComplete(true); // Enable the Cache value
  serviceConxt.setCachingOperationContext(cacheLastOperationContext);
  serviceConxt.setLastOperationContext(OperationContext);

You are explicitly creating new operationContext and and setting it to LastOperationContext . That is why you are getting null.

I used same code, just removed part which is setting setLastOperationContext .

It is working for me atleast for Out Message.

for In I am getting exception stating that "Attempted read on closed stream".

Starting with kekolab solution above, I wrote a method that can be used to log the outgoing and ingoing xmls:

After each _operation.execute(true) , call logPayload :

_operationClient.execute(true);
logPayload(_operationClient);

which is defined as:

  private void logPayload(OperationClient operationClient) {
    try {
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      operationClient.getMessageContext("Out").getEnvelope().serialize(out);
      logger.info(out.toString() + "\n\n");
      operationClient.getMessageContext("In").getEnvelope().serialize(out);
      logger.info(out.toString() + "\n\n");
    } catch (XMLStreamException e) {
      e.printStackTrace();
    } catch (AxisFault e) {
      e.printStackTrace();
    }
  }

Worked really nice for me and included the security tags inserted inside the soap enveloppe header as well.

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