Getting Raw XML From SOAPMessage in Java

前端 未结 9 1931
醉梦人生
醉梦人生 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: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.

提交回复
热议问题