gpt4 book ai didi

java - 当 namespace 定义在操作标记内时,如何处理 Castor 对 SOAP 消息的解码?

转载 作者:行者123 更新时间:2023-12-04 07:09:19 25 4
gpt4 key购买 nike

我正在开发基于 Spring-WS 的契约优先 Web 服务。我依赖于 Castor 编码(marshal)处理,但遇到了以下问题。

当在 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 文档的根而不继承顶级 namespace 声明,并且在上面的两种情况下,这会导致在您想要的 namespace 中,一个不是 - 所以一个可以根据您的架构进行验证,而另一个则不能。

最好的解决方案似乎是覆盖所有基地。使您的 XSD 具有 elementFormDefault="qualified",以要求所有元素都在 namespace 中。然后在 Castor 映射中的每个 map-to 元素中指定一个 ns-uri 和一个 ns-prefix。结果有点重,带有所有命名空间前缀,但它似乎在涉及到惰性客户端服务器组件中未记录的行为时变得不那么脆弱。

JAX-WS return empty lists也提出了一个很好的观点。 org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor 值得验证进出的内容。

关于java - 当 namespace 定义在操作标记内时,如何处理 Castor 对 SOAP 消息的解码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/626648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com