Keywords: JAX-WS | XML Tracing | SOAP Handler | Web Service Debugging | Java EE
Abstract: This article provides a comprehensive exploration of two core methods for tracing raw XML requests and responses in JAX-WS web services. It covers system property configuration for console logging and custom SOAP handler implementation for detailed message recording. The analysis includes implementation principles, use cases, and code examples to help developers choose optimal solutions while maintaining lightweight architecture without additional framework dependencies.
Introduction
In modern web service development, the ability to accurately trace and record raw XML format of SOAP messages is crucial for debugging, monitoring, and troubleshooting. JAX-WS, as part of the Java EE standard, provides multiple mechanisms to achieve this goal without relying on complex third-party frameworks.
System Property Configuration Method
By setting specific system properties, you can enable the built-in logging functionality of JAX-WS implementations to output all HTTP transport layer communication content to the console. This method is straightforward and suitable for quick debugging scenarios.
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");
System.setProperty("com.sun.xml.internal.ws.transport.http.HttpAdapter.dumpTreshold", "999999");
These properties target different JAX-WS implementation versions, and setting multiple properties ensures compatibility across various environments. The dumpTreshold property controls the size limit for output messages, and setting it to a large value ensures complete message recording.
Custom SOAP Handler Method
For scenarios requiring finer control, you can create custom message handlers by implementing the SOAPHandler interface. This approach offers greater flexibility to customize output formats, filter specific messages, or integrate with existing logging systems.
Handler Chain Configuration
First, add the custom handler to the endpoint's handler chain:
Endpoint ep = Endpoint.create(new WebserviceImpl());
List<Handler> handlerChain = ep.getBinding().getHandlerChain();
handlerChain.add(new SOAPLoggingHandler());
ep.getBinding().setHandlerChain(handlerChain);
ep.publish(publishURL);
SOAP Logging Handler Implementation
The custom SOAPLoggingHandler must implement the SOAPHandler<SOAPMessageContext> interface and override key methods:
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;
public class SOAPLoggingHandler implements SOAPHandler<SOAPMessageContext> {
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;
}
public void close(MessageContext messageContext) {
}
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("");
} catch (Exception e) {
out.println("Exception in handler: " + e);
}
}
}
Method Comparison and Selection
The system property configuration method is suitable for rapid prototyping and simple debugging, providing basic message tracing capabilities without writing additional code. The custom handler method offers more powerful control, enabling integration with enterprise logging systems, message filtering logic, or complex processing workflows.
Best Practice Recommendations
In production environments, it's recommended to redirect output to files or logging systems rather than directly to the console. This can be achieved by modifying the PrintStream instance or integrating with logging frameworks like SLF4J or Log4j. Additionally, be mindful of performance impacts, as excessive logging output may affect system performance in high-concurrency scenarios.
Conclusion
JAX-WS provides flexible and powerful message tracing mechanisms, allowing developers to choose appropriate methods based on specific requirements. Whether using simple system property configuration or complex custom handlers, these approaches effectively help developers understand and debug web service communication processes while maintaining code simplicity and framework lightweight characteristics.