Is it possible to get the raw XML payload using Metro web services framework?

…衆ロ難τιáo~ 提交于 2019-12-11 18:03:34

问题


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.

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.

Is there any way to do this?


回答1:


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;
    }
}

You can also use this call:

JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
Object payload = msg.getPayload(jaxbContext);

References:

  • JAX-WS : SOAP handler in server side
  • Adding JAX-WS handlers to web services and SOAP clients


来源:https://stackoverflow.com/questions/19102135/is-it-possible-to-get-the-raw-xml-payload-using-metro-web-services-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!