使用AXIS2创建的ADB存根在客户端获取原始XML SOAP响应

使用AXIS2创建的ADB存根在客户端获取原始XML SOAP响应,第1张

使用AXIS2创建的ADB存根客户端获取原始XML SOAP响应

按照joergl的建议,我使用“ SOAPHandler”将ADB存根更改为JAX-WS-
one,以按照以下说明记录请求,响应和错误:http ://www.mkyong.com/webservices/jax-ws/jax -ws-
soap-handler在客户端/

我的处理程序如下所示,用于使用log4j记录格式正确的XML:

public class RequestResponseHandler  implements SOAPHandler<SOAPMessageContext> {    private static Logger log = Logger.getLogger(RequestResponseHandler.class);    private Transformer transformer = null;    private documentBuilderFactory docBuilderFactory = null;    private documentBuilder docBuilder = null;    public RequestResponseHandler() {        try { transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "5"); docBuilderFactory = documentBuilderFactory.newInstance(); docBuilder = docBuilderFactory.newdocumentBuilder();        } catch (TransformerConfigurationException     | TransformerFactoryConfigurationError     | ParserConfigurationException e) { log.error(e.getMessage(), e);        }    }    @Override    public void close(MessageContext arg0) {    }    @Override    public boolean handleFault(SOAPMessageContext messageContext) {        log(messageContext);        return true;    }    @Override    public boolean handleMessage(SOAPMessageContext messageContext) {        log(messageContext);        return true;    }    private void log(SOAPMessageContext messageContext) {        String xml = "";        SOAPMessage msg = messageContext.getMessage();        ByteArrayOutputStream out = new ByteArrayOutputStream();        try { msg.writeTo(out); xml = out.toString("UTF-8");        } catch (Exception e) { log.error(e.getMessage(),e);        }        String direction = "";        Boolean outbound = (Boolean) messageContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);         if (outbound) {  direction += "Request: n";         } else {  direction += "Response: n";        }        log.info(direction + getXMLprettyPrinted(xml));         }    @Override    public Set<QName> getHeaders() {        return Collections.emptySet();    }    public String getXMLprettyPrinted(String xml) {        if (transformer == null || docBuilder == null) return xml;        InputSource ipXML = new InputSource(new StringReader(xml));        document doc;        try { doc = docBuilder.parse(ipXML); StringWriter stringWriter = new StringWriter(); StreamResult streamResult = new StreamResult(stringWriter); DOMSource domSource = new DOMSource(doc); transformer.transform(domSource, streamResult); return stringWriter.toString();        } catch (SAXException | IOException | TransformerException e) { log.error(e.getMessage(), e); return xml;        }    }}

另外,我想在应用程序代码中重用原始XML。因此,我不得不将此数据从SOAPHandler传输回我的客户端代码。如何做到这一点并不是太明显。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5561754.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存