Cannot create envelope from given source

16,344

If you are getting the exception while parsing the response from service, try sending the request from soap UI and check if it is working. If it is working in soap ui and not here, then you are not getting proper response, that can be because of improper request. In my case the issue was my endpoint url in code consists of "?wsdl". After removing it worked perfectly fine.

Share:
16,344
cookiedealer
Author by

cookiedealer

Updated on June 21, 2022

Comments

  • cookiedealer
    cookiedealer almost 2 years

    I am trying to consume a SOAP based web service with the class org.springframework.ws.client.core.WebServiceTemplate of Spring WS 2.2.2 release, like this:

    webServiceTemplate.setDefaultUri(uri);
    webServiceTemplate.setMessageSender(new SOAPMessageSenderWithAuth());
    res = (RESPONSE) webServiceTemplate.marshalSendAndReceive(request);
    

    The request is built with a class that has been generated from WSDL-files of the web service.

    The webservice has been successfully tested with SOAP UI, but when accessing it with Java the Exception "SoapMessageCreationException: Could not create message from InputStream: Unable to create envelope from given source (SAAJ0511)" and "Unable to create envelope from given source because the root element is not named 'Envelope' (SAAJ0514)" is thrown.

    Does anyone have any advice for this exception?

    Thanks in advance!

    The Spring bean for webServiceTemplate is defined as following:

    <bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate"
        p:marshaller-ref="jaxbMarshaller"
        p:unmarshaller-ref="jaxbMarshaller"
        p:defaultUri="...">
        <constructor-arg ref="messageFactory"/>
            <property name="messageSender">
              <bean class="org.springframework.ws.transport.http.HttpComponentsMessageSender">
                  <property name="credentials">
                      <bean class="org.apache.http.auth.UsernamePasswordCredentials">
                          <constructor-arg value="..."/>
                          <constructor-arg value="..."/>
                      </bean>
                  </property>
              </bean>
          </property>
        </bean>
    

    The Exception is:

    org.springframework.ws.soap.SoapMessageCreationException: Could not create message from InputStream: Unable to create envelope from given source: ; nested exception is com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Unable to create envelope from given source

    This is the class for the Web Service Client using the Spring WS Template:

    import javax.annotation.Resource;
    import org.apache.log4j.Logger;
    import org.springframework.ws.client.WebServiceIOException;
    import org.springframework.ws.client.core.WebServiceTemplate;
    import com.myproject.soap.client.services.SOAPWebServiceClient;
    
    /**
     *
     * @param <REQUEST>
     * @param <RESPONSE>
     */
    public class DefaultSOAPWebServiceClient<REQUEST, RESPONSE> implements SOAPWebServiceClient<REQUEST, RESPONSE>
    {
        private final static Logger LOG = Logger.getLogger(DefaultSOAPWebServiceClient.class.getName());
        @Resource(name = "webServiceTemplate")
        private WebServiceTemplate webServiceTemplate;
    
        @Override
        public RESPONSE sendAndReceive(final REQUEST request, final String uri)
        {
            LOG.info("SOAP URL-" + uri);
            LOG.info("REQUEST-" + request.toString());
            RESPONSE res = null;
    
            try
            {
                res = (RESPONSE) webServiceTemplate.marshalSendAndReceive(uri, request);
            }
            catch (final WebServiceIOException e)
            {
                e.printStackTrace();
                LOG.error("Service with URI: " + uri + " is unreachable");
            }
            return res;
        }
    }
    

    The method sendAndReceive is called like this:

    public MYDATAResponse createCustomer(final MYDATA request)
        {
            return (MYDATAResponse) soapWebServiceClient.sendAndReceive((REQUEST) request, getCreateCustomerURI());
        }