Getting Raw XML From SOAPMessage in Java

前端 未结 9 1874
醉梦人生
醉梦人生 2020-12-02 06:41

I\'ve set up a SOAP WebServiceProvider in JAX-WS, but I\'m having trouble figuring out how to get the raw XML from a SOAPMessage (or any Node) object. Here\'s a sample of t

相关标签:
9条回答
  • 2020-12-02 06:41

    if you have the client code then you just need to add the following two lines to get the XML request/response. Here _call is org.apache.axis.client.Call

    String request = _call.getMessageContext().getRequestMessage().getSOAPPartAsString();
    String response = _call.getMessageContext().getResponseMessage().getSOAPPartAsString();
    
    0 讨论(0)
  • 2020-12-02 06:44

    If you have a SOAPMessage or SOAPMessageContext, you can use a Transformer, by converting it to a Source via DOMSource:

                final SOAPMessage message = messageContext.getMessage();
                final StringWriter sw = new StringWriter();
    
                try {
                    TransformerFactory.newInstance().newTransformer().transform(
                        new DOMSource(message.getSOAPPart()),
                        new StreamResult(sw));
                } catch (TransformerException e) {
                    throw new RuntimeException(e);
                }
    
                // Now you have the XML as a String:
                System.out.println(sw.toString());
    

    This will take the encoding into account, so your "special characters" won't get mangled.

    0 讨论(0)
  • 2020-12-02 06:53

    for just debugging purpose, use one line code -

    msg.writeTo(System.out);

    0 讨论(0)
  • 2020-12-02 06:53

    It is pretty old thread but recently i had a similar issue. I was calling a downstream soap service, from a rest service, and I needed to return the xml response coming from the downstream server as is.

    So, i ended up adding a SoapMessageContext handler to get the XML response. Then i injected the response xml into servlet context as an attribute.

    public boolean handleMessage(SOAPMessageContext context) {
    
                // Get xml response
                try {
    
                    ServletContext servletContext =
                            ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getServletContext();
    
                    SOAPMessage msg = context.getMessage();
    
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    msg.writeTo(out);
                    String strMsg = new String(out.toByteArray());
    
                    servletContext.setAttribute("responseXml", strMsg);
    
                    return true;
                } catch (Exception e) {
                    return false;
                }
            }
    

    Then I have retrieved the xml response string in the service layer.

    ServletContext servletContext =
                    ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getServletContext();
    
            String msg = (String) servletContext.getAttribute("responseXml");
    

    Didn't have chance to test it yet but this approach must be thread safe since it is using the servlet context.

    0 讨论(0)
  • 2020-12-02 06:57

    Using Transformer Factory:-

    public static String printSoapMessage(final SOAPMessage soapMessage) throws TransformerFactoryConfigurationError,
                TransformerConfigurationException, SOAPException, TransformerException
        {
            final TransformerFactory transformerFactory = TransformerFactory.newInstance();
            final Transformer transformer = transformerFactory.newTransformer();
    
            // Format it
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    
            final Source soapContent = soapMessage.getSOAPPart().getContent();
    
            final ByteArrayOutputStream streamOut = new ByteArrayOutputStream();
            final StreamResult result = new StreamResult(streamOut);
            transformer.transform(soapContent, result);
    
            return streamOut.toString();
        }
    
    0 讨论(0)
  • 2020-12-02 07:02

    If you need formatting the xml string to xml, try this:

    String xmlStr = "your-xml-string";
    Source xmlInput = new StreamSource(new StringReader(xmlStr));
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    transformer.transform(xmlInput,
            new StreamResult(new FileOutputStream("response.xml")));
    
    0 讨论(0)
提交回复
热议问题