我正在编写一个在 Apache Tomcat 中运行的 Web 服务客户端。我需要获取请求/响应的 XML 负载,以便我可以记录它。
将字节转储到 stdOut 不是我想要的。我想在我的 Java 代码中以字节形式获取它,以便我可以按照我想要的方式记录它。
有什么办法吗?
最佳答案
是的,有。这是 JAX-WS 处理程序的主要用途之一。您不会获得原始字节形式的 XML 有效负载,而是经过格式化的;但是,如果您愿意,很容易再次将其转换为原始字节。一个例子:
public class MyCustomHandler implements SOAPHandler<SOAPMessageContext> {
@Override
public boolean handleMessage(SOAPMessageContext context) {
SOAPMessage msg = context.getMessage();
SOAPEnvelope env = msg.getSOAPPart().getEnvelope();
SOAPBody body = env.getBody();
// now when you have SOAP body you can do whatever you want...
return true;
}
}
你也可以使用这个调用:
JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
Object payload = msg.getPayload(jaxbContext);
引用资料:
关于xml - 是否可以使用 Metro Web 服务框架获取原始 XML 有效负载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19102135/