问题
是否有一种简单的方法(也就是说:不使用代理)来访问使用JAX-WS参考实现(JDK 1.5及更高版本中包含的那个)发布的Web服务的原始请求/响应XML?能够通过代码实现这一点是我需要做的。只需通过巧妙的日志记录配置将其记录到文件中就可以了。
我知道其他更复杂和完整的框架可能会这样做,但我希望尽可能简单,并且axis,cxf等都会增加我想要避免的大量开销。
谢谢!
#1 热门回答(222 赞)
以下选项可以将所有通信记录到控制台(从技术上讲,你只需要其中一个,但这取决于你使用的库,因此设置所有四个是更安全的选项)。你可以在示例中的代码中设置它,或者使用-D作为命令行参数,或者像Upendra所写的那样在环境变量中设置它。
System.setProperty("com.sun.xml.ws.transport.http.client.HttpTransportPipe.dump", "true");
System.setProperty("com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.dump", "true");
System.setProperty("com.sun.xml.ws.transport.http.HttpAdapter.dump", "true");
System.setProperty("com.sun.xml.internal.ws.transport.http.HttpAdapter.dump", "true");
有关详细信息,请参阅问题Tracing XML request/responses with JAX-WS when error occurs。
#2 热门回答(74 赞)
这是原始代码中的解决方案(感谢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);
}
}
}
#3 热门回答(47 赞)
在启动tomcat之前,请在Linux envs中设置JAVA_OPTS
as。然后启动Tomcat。你将在catalina.out
文件中看到请求和响应。
export JAVA_OPTS="$JAVA_OPTS -Dcom.sun.xml.ws.transport.http.client.HttpTransportPipe.dump=true"