【问题标题】:Mock SOAP service works in SoapUI but not with Spring Integration模拟 SOAP 服务在 SoapUI 中有效,但在 Spring 集成中无效
【发布时间】:2016-06-09 15:56:10
【问题描述】:

我按照指南 here 在 SoapUI 中设置了一个模拟 SOAP 服务。在 SoapUI 中执行 CurrencyConvertorSoap12 请求 1 时,我得到了适当的响应:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://www.webserviceX.NET/">
   <soap:Header/>
   <soap:Body>
      <web:ConversionRateResponse>
         <web:ConversionRateResult>2.34</web:ConversionRateResult>
      </web:ConversionRateResponse>
   </soap:Body>
</soap:Envelope>

现在我想使用 Spring Integration Web 服务出站网关对其进行测试。

我设置了一个 Web 服务测试(下面的 String requestXML 是 SoapUI 中的同一个 XML 请求):

@Test
public void webServiceTest() {

    AbstractApplicationContext context = new ClassPathXmlApplicationContext("Context.xml");

    DestinationResolver<MessageChannel> channelResolver = new BeanFactoryChannelResolver(context);

    String requestXml = "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:web=\"http://www.webserviceX.NET/\">"
                        + "<soap:Header/>"
                        + "<soap:Body>"
                        + "<web:ConversionRate>"
                        + "<web:FromCurrency>?</web:FromCurrency>"
                        + "<web:ToCurrency>?</web:ToCurrency>"
                        + "</web:ConversionRate>"
                        + "</soap:Body>"
                        + "</soap:Envelope>";

    Message<String> message = MessageBuilder.withPayload(requestXml).build();
    MessageChannel channel = channelResolver.resolveDestination("wsInChannel");

    channel.send(message);
}

Context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:int="http://www.springframework.org/schema/integration"
    xmlns:int-stream="http://www.springframework.org/schema/integration/stream"
    xmlns:int-ws="http://www.springframework.org/schema/integration/ws"
    xmlns:int-ftp="http://www.springframework.org/schema/integration/ftp"
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/integration
            http://www.springframework.org/schema/integration/spring-integration.xsd
            http://www.springframework.org/schema/integration/stream
            http://www.springframework.org/schema/integration/stream/spring-integration-stream.xsd
            http://www.springframework.org/schema/integration/ws
            http://www.springframework.org/schema/integration/ws/spring-integration-ws.xsd">

    <int:channel id="wsInChannel"/>

    <int:chain input-channel="wsInChannel" output-channel="stdoutChannel">
        <int-ws:header-enricher>
            <int-ws:soap-action value="http://www.webserviceX.NET/ConversionRate"/>
        </int-ws:header-enricher>
        <int-ws:outbound-gateway uri="http://localhost:8088/mockCurrencyConvertorSoap12"/>
    </int:chain>

    <int:channel id="stdoutChannel"/>
    <int-stream:stdout-channel-adapter
        id="consumer" channel="stdoutChannel" append-newline="true" />

</beans>

当我运行测试时,我在 SoapUI 中收到此错误:

Thu Jun 09 11:50:21 EDT 2016:ERROR:An error occurred [Missing operation for soapAction [http://www.webserviceX.NET/ConversionRate] and body element [{http://www.w3.org/2003/05/soap-envelope}Envelope] with SOAP Version [SOAP 1.1]], see error log for details

我做错了什么?

【问题讨论】:

  • 恕我直言,不从文件中读取 XML 是您正在做的一件错误的事情
  • @ThomasRS 这只是一个概念证明。这通常不是我在 XML 中阅读的方式。
  • 然后向我们展示您的 JAXB 实用程序;-) 我建议您也使用 CXF 进行评估并生成 Web 服务客户端;您还将获得服务器端点模拟。我已经举了一个例子here,它可以让你放弃SoapUI,至少在单元测试中是这样。
  • @ThomasRS 问题是关于如何使用 Spring Integration 正确发送 SOAP 请求,而不是用于读取 XML 的工具。

标签: spring soap spring-integration soapui


【解决方案1】:

和正文元素 [{http://www.w3.org/2003/05/soap-envelope}信封]

但是您发送整个 SOAP Envelope,而不仅仅是 Body 所请求的。

所以,你的requestXml 必须是这样的:

"<web:ConversionRate xmlns:web=\"http://www.webserviceX.NET/\">"
+ "<web:FromCurrency>?</web:FromCurrency>"
+ "<web:ToCurrency>?</web:ToCurrency>"
+ "</web:ConversionRate>"

别忘了用适当的货币替换? :-)

【讨论】:

  • 当我做出改变时,我得到了这个错误:[Fatal Error] :1:21: The prefix "web" for element "web:ConversionRate" is not bound. ERROR: 'The prefix "web" for element "web:ConversionRate" is not bound.'
  • 我刚刚在我的答案中添加了xmlns:web。查看更新的 XML。
  • 现在收到此错误:Cannot create message: incorrect content-type for SOAP version. Got application/soap+xml; charset=UTF-8, but expected text/xml
  • 这就是 Spring WS 生成的 content-type 标头。只需更改您的 SOAP-UI 以接受该标头而不是简单的 text/xml
  • 你必须将SaajSoapMessageFactory配置成SoapVersion.SOAP_11并注入&lt;int-ws:outbound-gateway&gt;
猜你喜欢
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-03
  • 1970-01-01
  • 2013-10-20
  • 1970-01-01
  • 2017-12-21
相关资源
最近更新 更多