本文介绍了如何打印SOAPMessage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SOAPMessage具有writeTo()方法,用于将其内容打印到流中。
但是如何将SOAPMessage内容写入StringBuffer?

SOAPMessage has writeTo() method which is used to print its content into a stream.But How I can write SOAPMessage content into StringBuffer?

代码行message.writeTo(System.out);必须修改..

code line "message.writeTo(System.out);" has to be modified..

public boolean handleMessage(SOAPMessageContext smc) {
     StringBuffer sbuf = new StringBuffer();
     sbuf.append("\n------------------------------------\n");
     sbuf.append("In SOAPHandler " + HandlerName + ":handleMessage()\n");

     Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

     if (outboundProperty.booleanValue()) {
        sbuf.append("\ndirection = outbound ");
     }
     else {
        sbuf.append("\ndirection = inbound ");
     }

     SOAPMessage message = smc.getMessage();      
     try {
        sbuf.append("\n");
        sbuf.append(message.toString());         
        //message.writeTo(System.out);         
        sbuf.append("\nMessage Desc:");         
        sbuf.append("\n");
     }
     catch (Exception e) {
        sbuf.append("Exception in SOAP Handler: " + e);
     }

     sbuf.append("Exiting SOAPHandler " + HandlerName + ":handleMessage()\n");
     sbuf.append("------------------------------------\n");
     logger.debug(sbuf.toString());
     return true;
  }


推荐答案

好的我已经解决了这个问题。修改后的代码块如下所示。

OK I have solved the question. Modified code block is as follow.

  public boolean handleMessage(SOAPMessageContext smc) {
     StringBuffer sbuf = new StringBuffer();
     sbuf.append("\n------------------------------------\n");
     sbuf.append("In SOAPHandler " + HandlerName + ":handleMessage()\n");

     Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

     if (outboundProperty.booleanValue()) {
        sbuf.append("\ndirection = outbound ");
     }
     else {
        sbuf.append("\ndirection = inbound ");
     }

     SOAPMessage message = smc.getMessage();      
     try {
        sbuf.append("\n");
        sbuf.append(message.toString()); 
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        message.writeTo(baos);                     
        sbuf.append("\nMessage Desc:"+baos.toString());         
        sbuf.append("\n");
     }
     catch (Exception e) {
        sbuf.append("Exception in SOAP Handler: " + e);
     }

     sbuf.append("Exiting SOAPHandler " + HandlerName + ":handleMessage()\n");
     sbuf.append("------------------------------------\n");
     logger.debug(sbuf.toString());
     return true;
  }

这篇关于如何打印SOAPMessage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 06:43