当操作标记内定义命名空间时,如何处理 SOAP 消息的 Castor 解组?

2024-01-12

我正在开发一个基于 Spring-WS 的契约优先 Web 服务。我依赖 Castor 封送,并且遇到了以下问题。

当 Envelope 标记中定义了“xmlns”命名空间时,请求将被接受,例如:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
                      xmlns="http://www.mycompany.com/MyService/schemas">
  <soap:Header/>
  <soap:Body>
    <doPlaceHoldRequest>
      <hold>
        <accountInfo>
          <accountNumber>123456789</accountNumber>
        </accountInfo>
        <extended>false</extended>
        <afterHours>false</afterHours>
        <amountSavings>1.00</amountSavings>
        <amountChecking>0.00</amountChecking>
      </hold>
    </doPlaceHoldRequest>
  </soap:Body>
</soap:Envelope>

但是,从 Spring-WS 提供的 .wsdl(从 XSD 生成)生成的 .NET 和 Java 客户端都按以下方式形成其请求:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header/>
  <soap:Body>
    <doPlaceHoldRequest 
                xmlns="http://www.mycompany.com/MyService/schemas">
      <hold>
        <accountInfo>
          <accountNumber>123456789</accountNumber>
        </accountInfo>
        <extended>false</extended>
        <afterHours>false</afterHours>
        <amountSavings>1.00</amountSavings>
        <amountChecking>0.00</amountChecking>
      </hold>
    </doPlaceHoldRequest>
  </soap:Body>
</soap:Envelope>

这会导致 Castor 抛出 Unmarshalling Exception。如何让 Castor 识别这些消息有效?我的 WSDL(或我用来自动生成它的 XSD)可能是错误的吗?


我在使用第一个 Spring-WS/Castor Web 服务时一次又一次地遇到这个问题。据我所知,某些组件以非命名空间感知的方式提取有效负载。换句话说,像 doPlaceHoldRequest 这样的节点成为 XML 文档的根,而不继承顶级命名空间声明,并且在上面的两种情况下,这会导致以下情况:is在您想要的命名空间中,而一个则不是 - 因此一个可以根据您的架构进行良好的验证,而另一个则不能。

最好的解决方案似乎是涵盖所有基础。让您的 XSD 具有 elementFormDefault="qualified",以要求所有元素都位于命名空间中。然后在 Castor 映射中的每个 map-to 元素中指定 ns-uri 和 ns-prefix。结果有点重,带有所有名称空间前缀,但对于惰性客户端来说,它似乎不那么脆弱and服务器组件中未记录的行为。

JAX-WS 返回空列表 https://stackoverflow.com/questions/3601113/jax-ws-return-empty-lists/3602475#3602475也提出了一个很好的观点。org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor值得验证输入和输出的内容。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当操作标记内定义命名空间时,如何处理 SOAP 消息的 Castor 解组? 的相关文章

随机推荐