35

Using spring ws to get the StreamResult as below

StreamSource source = new StreamSource(new StringReader(MESSAGE));
StreamResult result = new StreamResult(System.out);
webServiceTemplate.sendSourceAndReceiveToResult("http://someUri", 
                source, new SoapActionCallback("someCallBack"), result); 
return result;

I get the result, But I want to extract it to some sort of xml or even as a string (Just want to see the contents in order to generate the response).

How can I do this?

user1609085
  • 855
  • 3
  • 17
  • 33
  • 1
    http://stackoverflow.com/questions/22939440/how-to-get-soap-response-from-webservicetemplate/22939680#22939680 might help – VirtualTroll Apr 22 '14 at 12:56

4 Answers4

58

Try this one:

try {
    StreamSource source = new StreamSource(new StringReader("<xml>blabla</xml>"));
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    transformer.transform(source,result);
    String strResult = writer.toString();
} catch (Exception e) {
    e.printStackTrace();
}
Jorge Campos
  • 22,647
  • 7
  • 56
  • 87
Asif Bhutto
  • 3,916
  • 1
  • 24
  • 21
5

You can get the reader of your StreamSource by using getReader(). You should then be able to use read(char[] cbuf) to write the contents of the stream to a character array which can easily be converted into a string and printed to the console if you wish.

Lex Webb
  • 2,772
  • 2
  • 21
  • 36
  • I am interested in only StreamResult – user1609085 Apr 22 '14 at 12:53
  • StreamResult is a writer class, you cannot get any data from a writer class as it passes on all data passed to it. And does not store the data. See the definition here: http://docs.oracle.com/javase/7/docs/api/javax/xml/transform/stream/StreamResult.html – Lex Webb Apr 22 '14 at 13:00
3

If none of these works, try this

System.out.println(result.getOutputStream().toString());

Assuming you have this kind of structure ,

private static StreamResult printSOAPResponse(SOAPMessage soapResponse) throws Exception {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    Source sourceContent = soapResponse.getSOAPPart().getContent();
    System.out.print("\nResponse SOAP Message = ");
    StreamResult result = new StreamResult(System.out);
    transformer.transform(sourceContent, result);
    return result;
}

You can try this way , although the same thing, wanted to point it out clearly

System.out.println(printSOAPResponse(soapResponse).getOutputStream().toString());
murasing
  • 412
  • 6
  • 15
3

If you use Spring you could also use this way:

    import org.springframework.core.io.Resource;
    import org.apache.commons.io.IOUtils;
    ....    
    @Value("classpath:/files/dummyresponse.xml")
    private Resource dummyResponseFile;
    ....
    public String getDummyResponse() {
        try {
            if (this.dummyResponse == null)
                dummyResponse = IOUtils.toString(dummyResponseFile.getInputStream(),StandardCharsets.UTF_8);
        }  catch (IOException e) {
            logger.error("Fehler in Test-Service: {}, {}, {}", e.getMessage(), e.getCause(), e.getStackTrace());
            throw new RuntimeException(e);
        }
        return dummyResponse;
    }
the hand of NOD
  • 1,639
  • 1
  • 18
  • 30