简体   繁体   中英

Java - Consume SOAP Web Service from String XML Envelope

Using SoapUI I have built the following XML envelope and encapsulated it in a String.

ProcessJournal is a method which has no parameters, and returns a string.

String soapText = "<Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:upd=\"http://webservices.vm.vmc/UpdateCMA/\">" +
                    "<Header/>" +
                     "<Body>" +
                       "<upd:ProcessJournal/>" +
                     "</Body>" +
                  "</Envelope>";

Now... I simply want to invoke the web service defined above. And have found some sample code to do so

                // Create SoapMessage
                MessageFactory msgFactory     = MessageFactory.newInstance();
                SOAPMessage message           = msgFactory.createMessage();
                SOAPPart soapPart             = message.getSOAPPart();

                // Load the SOAP text into a stream source
                byte[] buffer                 = soapText.getBytes();
                ByteArrayInputStream stream   = new ByteArrayInputStream(buffer);
                StreamSource source           = new StreamSource(stream);

                // Set contents of message 
                soapPart.setContent(source);

                // -- DONE
                message.writeTo(System.out);


                //Try accessing the SOAPBody
                SOAPBody soapBody = message.getSOAPBody();

The problem is, at message.getSOAPBody();, I get an error

XML-22103: (Fatal Error) DOMResult can not be this kind of node.
Apr 16, 2013 12:05:06 PM com.sun.xml.internal.messaging.saaj.soap.EnvelopeFactory createEnvelope
SEVERE: SAAJ0511: Unable to create envelope from given source

All the various samples I find, end up having the same type of error when getSOAPBody() is executed.

I don't know whether you ever found a solution to this. I've just recently been seeing the exact same error and it was due to a TransformerFactory not being set.

I used the TransformerFactory from the Saxon library - the jar for which can be obtained here .

I then set a system property which referenced the Saxon TransformerFactory:

System.setProperty("javax.xml.transform.TransformerFactory",    
        "net.sf.saxon.TransformerFactoryImpl");

The error then disappeared when I re-ran my code.

The above is just one way to set the TransformerFactory. There are a number of other ways to do this which I found here: stackoverflow.com/questions/11314604/

It looks like the problem was with the namespace prefixing in your soapText. I was able to execute your example without error and without manually setting any TransformerFactory simply by modifying your soapText as follows:

String soapText = 
     "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:upd=\"http://webservices.vm.vmc/UpdateCMA/\">" +
        "<soapenv:Header/>" +
        "<soapenv:Body>" +
          "<upd:ProcessJournal/>" +
        "</soapenv:Body>" +
     "</soapenv:Envelope>";

Note the added soapenv: prefix in all the Soap elements in contrast to the upd prefix in your Soap service namespace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM