Can I get the raw XML payload using the Metro web services infrastructure?

I am writing a web service client that runs on Apache Tomcat. I need to get the XML payload for the request / response so that I can register it.

Dropping bytes in stdOut is not what I want. I want to get it as bytes in my Java code so that I can write it the way I want.

Is there any way to do this?

0
source share
1 answer

Yes there is. This is one of the main goals of the JAX-WS handlers. You will not get the XML payload as raw bytes, but formatted; however, if you want, it's easy to re-include it in raw bytes. 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); 

Literature:

+1
source

All Articles