javax.xml.ws.handler.soap.SOAPHandler
如果您需要自定义日志记录或以不同方式处理 SOAP 消息,您可以实现自己的,方法如下:
MyService ws = new MyService().getMyServiceSoap12();
BindingProvider wsBindingProvider = (BindingProvider) ws;
// Get a copy of the handler chain for a protocol binding instance
List<Handler> handlers =
wsBindingProvider.getBinding().getHandlerChain();
// Add your handler(s)
handlers.add(new MySoapMessageLogger());
// We need to setHandlerChain because setHandlerChain
// returns a copy of List<Handler>
wsBindingProvider.getBinding().setHandlerChain(handlers);
MySoapMessageLogger 可能如下所示:
public class MySoapMessageLogger implements SOAPHandler<SOAPMessageContext> {
private static final Logger logger =
LoggerFactory.getLogger(MySoapMessageLogger.class);
@Override
public boolean handleMessage(SOAPMessageContext context) {
Boolean isRequest = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
// Retrieve the message contents
ByteArrayOutputStream stream = new ByteArrayOutputStream();
context.getMessage().writeTo(stream);
// You've got your XML message and can log it right away or
// beautify it with some library before sending to log
logger.trace((isRequest ? "Request:" : "Response:") + stream.toString());
return true;
}
/* For the logging purposes the following methods can be leaved as stubs */
@Override
public Set<QName> getHeaders() {
return null;
}
@Override
public boolean handleFault(SOAPMessageContext context) {
return false;
}
@Override
public void close(MessageContext context) {
}
}