首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当dataFormat为POJO时获取请求和响应SOAP消息

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

Stack Overflow用户
提问于 2017-05-25 15:29:45
回答 1查看 463关注 0票数 0

我正在使用WebServices和Apache Camel,并使用dataFormat作为POJO来请求该for服务。我可以成功地调用服务,但我希望记录请求和响应SOAP消息,但我无法记录这些消息,因为SOAP消息是由CXF从POJO类创建的。

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

EN

回答 1

Stack Overflow用户

发布于 2017-05-30 22:13:16

这是我如何在我的项目中记录soap请求和响应的示例,希望这能有所帮助

代码语言:javascript
复制
BindingProvider bindingProvider = ((BindingProvider) PortType);
List<Handler> handlerChain = bindingProvider.getBinding().getHandlerChain();
handlerChain.add(new SOAPLoggingHandler());    
bindingProvider.getBinding().setHandlerChain(handlerChain);

和类SOAPLoggingHandler

代码语言:javascript
复制
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);
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44174799

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档