Getting raw XML response from Java web service client

17,169

Solution 1

You can do it using

javax.xml.ws.handler.soap.SOAPHandler<javax.xml.ws.handler.soap.SOAPMessageContext>

you can simply get message using SOAPMessageContext#getMessage() and convert message to String using method

   public static String getXmlMessage(SOAPMessage message) throws Exception
   {
         ByteArrayOutputStream os = new ByteArrayOutputStream();
         message.writeTo(os);
         final String encoding = (String) message.getProperty(SOAPMessage.CHARACTER_SET_ENCODING);
         if (encoding == null)
         {
             return new String(os.toByteArray());
         }
         else
         {
            return new String(os.toByteArray(), encoding);    
         }
   }  

Also you can read here about SOAP handler on client side
Article

Solution 2

It's not widely documented, but you can use the Dispatch interface to implement JAXWS clients which work directly w/ the XML. Here and here are some articles for getting started.

Share:
17,169
FrustratedWithFormsDesigner
Author by

FrustratedWithFormsDesigner

SOreadytohelp

Updated on June 04, 2022

Comments

  • FrustratedWithFormsDesigner
    FrustratedWithFormsDesigner almost 2 years

    I am trying to get the raw XML response from a web service, instead of the usual set of POJOs.

    I am using a webservice client that I generated (so I have access to the client's code) from a WSDL and some schemas. The client is generated in RAD 7.5, I think using JAX-WS. I've been looking at the client code itself, but I'm not even sure if the client code ever handles raw XML or if it passes it off to other libraries.

  • FrustratedWithFormsDesigner
    FrustratedWithFormsDesigner over 11 years
    Ok, though I'm not sure how I can return this string back to my main application.
  • Ilya
    Ilya over 11 years
    I am using this construction in my project for print request/response in logs.
  • Ilya
    Ilya over 11 years
    You can create global variable ArrayList, and put all String to it
  • FrustratedWithFormsDesigner
    FrustratedWithFormsDesigner over 11 years
    That's going to be a little awkward in my situation. Is there a more graceful way of doing it?
  • abel.matos
    abel.matos about 3 years
    Two last links like broken.