java 无法从给定来源创建信封

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33095643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:10:17  来源:igfitidea点击:

Cannot create envelope from given source

javaspringweb-servicessoap

提问by cookiedealer

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

我正在尝试使用Spring WS 2.2.2 版本的org.springframework.ws.client.core.WebServiceTemplate类使用基于 SOAP 的 Web 服务,如下所示:

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.

该请求是使用从 Web 服务的 WSDL 文件生成的类构建的。

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.

Web 服务已通过 SOAP UI 成功测试,但在使用 Java 访问时出现异常“ SoapMessageCreationException:无法从 InputStream 创建消息:无法从给定源创建信封 (SAAJ0511)”和“无法从给定源创建信封,因为根元素未命名为“信封”(SAAJ0514)”被抛出。

Does anyone have any advice for this exception?

有没有人对此例外有任何建议?

Thanks in advance!

提前致谢!

The Spring bean for webServiceTemplateis defined as following:

webServiceTemplate的 Spring bean定义如下:

<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

org.springframework.ws.soap.SoapMessageCreationException:无法从 InputStream 创建消息:无法从给定源创建信封:;嵌套异常是 com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl:无法从给定源创建信封

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

这是使用 Spring WS 模板的 Web 服务客户端的类:

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 sendAndReceiveis called like this:

方法sendAndReceive是这样调用的:

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

回答by Viswa Teja Kuncham

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.

如果您在解析来自服务的响应时遇到异常,请尝试从soap UI 发送请求并检查它是否正常工作。如果它在soap ui 中运行而不是在这里运行,那么您没有得到正确的响应,这可能是因为请求不正确。就我而言,问题是代码中的端点 url 由“?wsdl”组成。删除后它工作得很好。