是否可以使用Metro Web服务框架获取原始XML有效负载? [英] Is it possible to get the raw XML payload using Metro web services framework?
问题描述
我正在编写一个在Apache Tomcat中运行的Web服务客户端.我需要获取请求/响应的XML有效负载,以便可以对其进行记录.
I'm writing a web service client that runs in Apache Tomcat. I need to get the XML payload for the request/response so that I can log it.
将字节转储到stdOut不是我想要的.我想在Java代码中以字节为单位获取它,以便可以按自己的方式对其进行记录.
Dumping the bytes to stdOut is not what I want. I want to get it as bytes in my Java code, so that I can log it the way that I want.
有什么办法吗?
推荐答案
是的.它是JAX-WS处理程序的主要目的之一.您不会以原始字节的形式获取XML有效负载,而是对其进行了格式化;但是,如果您愿意,可以很容易地再次将其转换为原始字节.一个例子:
Yes, there is. It is one of main purposes of JAX-WS handlers. You will not get XML payload as raw bytes, but formatted; however if you want it is easy to turn it into raw bytes again. An example:
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);
参考文献:
- JAX-WS : SOAP handler in server side
- Adding JAX-WS handlers to web services and SOAP clients
这篇关于是否可以使用Metro Web服务框架获取原始XML有效负载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!