0

我阅读了很多用于记录 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);
  }
  }
}
4

1 回答 1

1

您可以实现一个 MessageHandler,它应该可以工作:

公共类 MyLoggingHandler 实现 MessageHandler { private static final Logger LOGGER = LoggerFactory.getLogger(MyLoggingHandler.class);

public boolean handleMessage(MessageHandlerContext mhc) {
    Message m = mhc.getMessage().copy();

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    XMLStreamWriter writer = XMLStreamWriterFactory.create(stream);
    try {
        m.writeTo(writer);
        if(stream.size() > 0) {
            LOGGER.debug(">> Message << \n{}", new String(stream.toByteArray()));
        }
    } catch (XMLStreamException e) {
        LOGGER.error("Could not log the incoming message, something wrong with the xml?", e);
        return false;
    }
    return true;
}

public boolean handleFault(MessageHandlerContext mhc) {

    return true;
}

public void close(MessageContext messageContext) {
}

public Set getHeaders() {
    return null;
}

}

于 2013-04-12T19:18:20.097 回答