我正在使用 spring-ws 2.2.3 构建合同优先的 SOAP Web 服务。我的 XML 模式使用扩展,导致xsi:type=
XML 请求中的属性。由于某些响应可能非常大(30MB),因此我使用 AxiomSoapMessageFactory 而不是默认的 SaajSoapMessageFactory,如Spring WS docs中所建议的那样。我使用 PayloadValidatingInterceptor 验证传入请求:
PayloadValidatingInterceptor interceptor = new PayloadValidatingInterceptor();
interceptor.setSchema(new ClassPathResource("format-service.xsd"));
interceptor.setValidateRequest(true);
interceptor.setValidateResponse(false);
我的问题是我得到虚假的验证错误,这取决于在 XML 中声明命名空间的位置:如果它在有效负载中声明,那么一切正常:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" >
<soapenv:Header/>
<soapenv:Body>
<sch:formatRequest xmlns:sch="http://example.com/springws/extension/schema">
<sch:value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="sch:currencyType">
<sch:amount>1000</sch:amount>
<sch:currency>EUR</sch:currency>
</sch:value>
</sch:formatRequest>
</soapenv:Body>
</soapenv:Envelope>
但是一旦我将sch
命名空间声明移到信封:
<soapenv:Envelope xmlns:sch="http://example.com/springws/extension/schema" ...>
验证失败,我收到一个 SOAP 错误:
<faultcode>soapenv:Client</faultcode>
<faultstring xml:lang="en">Validation error</faultstring>
<detail>
<spring-ws:ValidationError xmlns:spring-ws="http://springframework.org/spring-ws">UndeclaredPrefix: Cannot resolve 'sch:currencyType' as a QName: the prefix 'sch' is not declared.</spring-ws:ValidationError>
<spring-ws:ValidationError xmlns:spring-ws="http://springframework.org/spring-ws">cvc-elt.4.1: The value 'sch:currencyType' of attribute 'http://www.w3.org/2001/XMLSchema-instance,type' of element 'sch:value' is not a valid QName.</spring-ws:ValidationError>
<spring-ws:ValidationError xmlns:spring-ws="http://springframework.org/spring-ws">cvc-type.2: The type definition cannot be abstract for element sch:value.</spring-ws:ValidationError>
<spring-ws:ValidationError xmlns:spring-ws="http://springframework.org/spring-ws">UndeclaredPrefix: Cannot resolve 'sch:currencyType' as a QName: the prefix 'sch' is not declared.</spring-ws:ValidationError>
<spring-ws:ValidationError xmlns:spring-ws="http://springframework.org/spring-ws">cvc-attribute.3: The value 'sch:currencyType' of attribute 'xsi:type' on element 'sch:value' is not valid with respect to its type, 'QName'.</spring-ws:ValidationError>
<spring-ws:ValidationError xmlns:spring-ws="http://springframework.org/spring-ws">cvc-complex-type.2.1: Element 'sch:value' must have no character or element information item [children], because the type's content type is empty.</spring-ws:ValidationError>
</detail>
在我看来,这就像 AxiomSoapMessageFactory / Axiom 实现中的一个错误(名称空间上下文丢失),因为在使用 SaajSoapMessageFactory 时两个请求都可以正常验证。请注意,验证仅在xsi:type=
属性上失败。对于元素,可以正确识别相同的命名空间。
由于大量响应的高内存消耗,我无法使用 SaajSoapMessageFactory。我在Spring 论坛和SO上发现了类似的问题,但没有解决方案。感谢帮助!