1

I wrote method, which generate soap message from java string:

private SOAPMessage createRequest(String msg) {
    SOAPMessage request = null;
    try {
        MessageFactory msgFactory = MessageFactory.newInstance();
        request = factory.createMessage();

        SOAPPart msgPart = request.getSOAPPart();
        SOAPEnvelope envelope = msgPart.getEnvelope();
        SOAPBody body = envelope.getBody();

        StreamSource _msg = new StreamSource(new StringReader(msg));
        msgPart.setContent(_msg);

        request.saveChanges();
    } catch(Exception ex) {
       ex.printStackTrace();
    }
}

And, after that, I try generate some message. For example:

createRequest("test message");

But here - request.saveChanges(); I catch this exception: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Error during saving a multipart message

Where is my mistake?

Giymose
  • 201
  • 1
  • 6
  • 21
  • 2
    You're not reading the whole exception stack, the most important part should be org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog, which means your msg should be valid XML in the first place. – Danio Sep 18 '15 at 08:04
  • 1
    Possible duplicate of [How to convert a string to a SOAPMessage in Java?](https://stackoverflow.com/questions/13614508/how-to-convert-a-string-to-a-soapmessage-in-java) – Vadzim Jun 06 '19 at 14:02

1 Answers1

2

That is because you are not passing a correct protocol formatted message. Your code doesn't specify which SOAP protocol you want to use, that means it creates a message factory for SOAP 1.1 messages.

Thus, you would need to pass a correct SOAP1.1 message. I replicated your method like this:

private static SOAPMessage createRequest(String msg) {
        SOAPMessage request = null;
        try {
            MessageFactory msgFactory = MessageFactory
                    .newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
            request = msgFactory.createMessage();

            SOAPPart msgPart = request.getSOAPPart();
            SOAPEnvelope envelope = msgPart.getEnvelope();
            SOAPBody body = envelope.getBody();

            javax.xml.transform.stream.StreamSource _msg = new javax.xml.transform.stream.StreamSource(
                    new java.io.StringReader(msg));
            msgPart.setContent(_msg);

            request.saveChanges();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return request;
    }

and I call it using this string:

String soapMessageString = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Header/><SOAP-ENV:Body></SOAP-ENV:Body></SOAP-ENV:Envelope>";
createRequest(soapMessageString);

and It works.

William Kinaan
  • 28,059
  • 20
  • 85
  • 118