dataFormat为POJO时获取请求和响应SOAP消息

问题描述:

我正在使用WebServices和Apache Camel并使用dataFormat作为POJO来请求该webservice。我成功地能够调用服务,但我想记录请求和响应SOAP消息,但我无法访问,因为SOAP消息是由CXF从POJO类创建的。dataFormat为POJO时获取请求和响应SOAP消息

当dataFormat是POJO时,有什么方法可以记录请求和响应SOAP消息吗?

+0

看CXF拦截日志消息。 – Namphibian

我这是怎么记录的SOAP请求,并在我的项目响应的例子,希望这有助于

BindingProvider bindingProvider = ((BindingProvider) PortType); 
List<Handler> handlerChain = bindingProvider.getBinding().getHandlerChain(); 
handlerChain.add(new SOAPLoggingHandler());  
bindingProvider.getBinding().setHandlerChain(handlerChain); 

和类SOAPLoggingHandler

public class SOAPLoggingHandler implements SOAPHandler<SOAPMessageContext> { 

    // change this to redirect output if desired 
    public static Logger logger = Logger.getLogger("GetCustomerDataLand"); 

    public Set<QName> getHeaders() { 
     return null; 
    } 

    public boolean handleMessage(SOAPMessageContext smc) { 
     logToSystemOut(smc); 
     return true; 
    } 

    public boolean handleFault(SOAPMessageContext smc) { 
     logToSystemOut(smc); 
     return true; 
    } 

    // nothing to clean up 
    public void close(MessageContext messageContext) { 
    } 

    /* 
    * Check the MESSAGE_OUTBOUND_PROPERTY in the context to see if this is an 
    * outgoing or incoming message. Write a brief message to the print stream 
    * and output the message. The writeTo() method can throw SOAPException or 
    * IOException 
    */ 
    private void logToSystemOut(SOAPMessageContext smc) { 
     Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); 

     if (outboundProperty.booleanValue()) { 
      logger.info(new SimpleDateFormat("yyyy-MM-dd HH:mm:sss").format(new Date()) + "\nOutbound message:"); 
     } else { 
      logger.info(new SimpleDateFormat("yyyy-MM-dd HH:mm:sss").format(new Date()) + "\nInbound message:"); 
     } 

     SOAPMessage message = smc.getMessage(); 
     try { 

      ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
      message.writeTo(stream); 
      String msg = new String(stream.toByteArray(), "utf-8"); 

      logger.info(toPrettyString(msg)); 
      // message.writeTo(out); 
      logger.info(""); // just to add a newline 
     } catch (Exception e) { 
      logger.info(new SimpleDateFormat("yyyy-MM-dd HH:mm:sss").format(new Date()) + "Exception in handler: " 
        + org.apache.commons.lang.exception.ExceptionUtils.getStackTrace(e)); 
     } 
    } 

    public String toPrettyString(String xml) { 

     try { 
      final InputSource src = new InputSource(new StringReader(xml)); 
      final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src) 
        .getDocumentElement(); 
      final Boolean keepDeclaration = Boolean.valueOf(xml.startsWith("<?xml")); 
      final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); 
      final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); 
      final LSSerializer writer = impl.createLSSerializer(); 

      writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); 
      writer.getDomConfig().setParameter("xml-declaration", keepDeclaration); 
      return writer.writeToString(document); 
     } catch (Exception e) { 
      throw new RuntimeException(e); 
     } 
    } 
}