有没有一种简单的方法(又名:不使用代理)来访问使用 JAX-WS 参考实现(JDK 1.5 和更好的版本)发布的 Web 服务的原始请求/响应 XML?能够通过代码做到这一点是我需要做的。只需通过巧妙的日志配置将其记录到文件中就足够了。
我知道存在其他更复杂和更完整的框架可以做到这一点,但我想让它尽可能简单,axis、cxf 等都会增加我想避免的相当大的开销。
谢谢!
这是原始代码中的解决方案(感谢 stjohnroe 和 Shamik):
Endpoint ep = Endpoint.create(new WebserviceImpl()); List<Handler> handlerChain = ep.getBinding().getHandlerChain(); handlerChain.add(new SOAPLoggingHandler()); ep.getBinding().setHandlerChain(handlerChain); ep.publish(publishURL);
SOAPLoggingHandler 所在的位置(从链接的示例中提取):
package com.myfirm.util.logging.ws; import java.io.PrintStream; import java.util.Map; import java.util.Set; import javax.xml.namespace.QName; import javax.xml.soap.SOAPMessage; import javax.xml.ws.handler.MessageContext; import javax.xml.ws.handler.soap.SOAPHandler; import javax.xml.ws.handler.soap.SOAPMessageContext; /* * This simple SOAPHandler will output the contents of incoming * and outgoing messages. */ public class SOAPLoggingHandler implements SOAPHandler<SOAPMessageContext> { // change this to redirect output if desired private static PrintStream out = System.out; 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()) { out.println("\nOutbound message:"); } else { out.println("\nInbound message:"); } SOAPMessage message = smc.getMessage(); try { message.writeTo(out); out.println(""); // just to add a newline } catch (Exception e) { out.println("Exception in handler: " + e); } } }