我阅读了很多用于记录 Web 服务请求/响应的教程,发现它们都在做同样的事情,但我无法记录请求,但事情似乎是正确的,它甚至没有抛出错误。在这里我给出了我的示例代码,如果有人能告诉我错误在哪里以及错误是什么,那就太好了。
My web service
@WebService(endpointInterface = "com.sample.ws.SampleWS", serviceName = "SampleWS")
@HandlerChain(file="/com/sample/ws/handler.xml")
public class SampleWSImpl implements SampleWS {
public SampleWSImpl() {}
public String hello(String s) {
return "Hello "+s;
}
}
my handler.xml.
<?xml version="1.0" encoding="UTF-8"?>
<handler-chains xmlns="http://java.sun.com/xml/ns/javaee">
<handler-chain>
<handler>
<handler-class>com.sample.LoggingHandler</handler-class>
</handler>
</handler-chain>
</handler-chains>
my log handler class .
public class LoggingHandler implements SOAPHandler<SOAPMessageContext> {
@Override
public Set<QName> getHeaders() {
return null;
}
@Override
public void close(MessageContext context) {
}
@Override
public boolean handleFault(SOAPMessageContext context) {
logToSystemOut(context);
return true;
}
@Override
public boolean handleMessage(SOAPMessageContext context) {
logToSystemOut(context);
return true;
}
private void logToSystemOut(SOAPMessageContext smc) {
Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
try {
if (!outboundProperty.booleanValue()) {
SOAPMessage message = smc.getMessage();
System.out.println("Incoming message:");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
message.writeTo(stream);
System.out.println(stream.toString());
System.out.println("=====================================");
}
}
catch (Exception e) {
System.out.println("Exception in handler: " + e);
}
}
}