【发布时间】:2024-01-29 15:15:02
【问题描述】:
我正在使用 Apache CXF 开发 JAX-WS 服务,该服务从两个来源获取其类型定义。 XSD 模式文件在 .../types/ 命名空间中定义了各种类型,并且存在带有 JAXB 注释的匹配 java 类。端点在 Java 接口中定义,并在 .../service/ 命名空间内使用与 @WebService 相关的注释。 WSDL 由 Apache CXF 生成,它使用来自 XSD 模式文件的类型定义以及从 @WebService 端点获取的请求/响应消息和参数的生成类型定义。
我遇到了以下关于 .../service/ 命名空间内的 Apache CXF 生成类型之一的验证器错误。
Caused by: org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 10; cvc-elt.1: Cannot find the declaration of element 'ser:subscriberId'.
端点在 .../service/ 命名空间中是这样定义的:
@WebService(name="gatewayService",
targetNamespace="http://www.mydomain.com/gateway/schema/service/")
public interface GatewayEndpoint {
// ...
@WebMethod(operationName="addService")
@XmlElement(required=true) public Response addService(
@XmlElement(required=true) @WebParam(name="subscriberId") long subscriberId,
@XmlElement(required=true) @WebParam(name="service") Service service)
throws GatewayException;
// ...
}
JAX-WS 端点在 spring 配置文件中定义如下:
<!-- Apache CXF endpoint -->
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<jaxws:endpoint id="gatewayndpoint" implementor="#gatewayEndpointImpl" address="/gateway">
<jaxws:schemaLocations>
<jaxws:schemaLocation>classpath:/schemas/gateway_schema.xsd</jaxws:schemaLocation>
</jaxws:schemaLocations>
<jaxws:properties>
<entry key="schema-validation-enabled" value="true" />
</jaxws:properties>
</jaxws:endpoint>
gateway_schema.xsd 包含各种 complexType 定义,其中 .../types/ 命名空间中的 Service 定义:
<xs:complexType name="Service">
<xs:sequence>
<xs:element name="name">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="10"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="deviceLimit" type="xs:int"/>
<xs:element maxOccurs="unbounded" minOccurs="0" name="subscription" nillable="true" type="gateway:Subscription"/>
<xs:element maxOccurs="unbounded" minOccurs="0" name="package" nillable="true" type="gateway:Package"/>
</xs:sequence>
</xs:complexType>
而匹配的 JAXB 注解类是:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Service")
public class Service {
@XmlElement(required=true)
private String name;
private int deviceLimit;
@XmlElement(name="subscription", nillable=true)
private List<Subscription> subscriptions;
@XmlElement(name="package", nillable=true)
private List<Package> packages;
//.. getters and setters
}
一切都打包在一个war文件中,部署后生成的WSDL如下所示:
<?xml version="1.0" ?>
<wsdl:definitions
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:tns="http://www.mydomain.com/gateway/schema/service/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:ns2="http://schemas.xmlsoap.org/soap/http"
xmlns:ns1="http://www.mydomain.com/gateway/schema/services/"
name="GatewayService"
targetNamespace="http://www.mydomain.com/gateway/schema/service/">
<wsdl:types>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:gateway="http://www.mydomain.com/gateway/schema/types/"
elementFormDefault="qualified"
targetNamespace="http://www.mydomain.com/gateway/schema/types/"
version="1.0">
<!--
Various type definitions available in the gateway_schema.xsd
Among them the typ:Service definition
-->
</xs:schema>
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.mydomain.com/gateway/schema/services/"
xmlns:ns0="http://www.mydomain.com/gateway/schema/types/"
attributeFormDefault="unqualified"
elementFormDefault="qualified"
targetNamespace="http://www.mydomain.com/gateway/schema/services/">
<xsd:import namespace="http://www.mydomain.com/gateway/schema/types/"></xsd:import>
<!--
Definitions generated by Apache CXF
-->
<xsd:element name="addService" type="tns:addService"></xsd:element>
<xsd:complexType name="addService">
<xsd:sequence>
<xsd:element name="subscriberId" type="xsd:long"></xsd:element>
<xsd:element name="service" type="ns0:Service"></xsd:element>\
</xsd:sequence>
</xsd:complexType>
<!-- .... -->
</xsd:schema>
</wsdl:types>
<wsdl:message name="addService">
<wsdl:part element="tns:addService" name="parameters"></wsdl:part>
</wsdl:message>
<!... rest of messages, portTypes, bindings and wsdl:service -->
</wsdl:definitions>
SoapUI 为该服务生成的请求是这样的:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://www.mydomain.com/gateway/schema/service/" xmlns:typ="http://www.mydomain.com/gateway/schema/types/">
<soapenv:Header/>
<soapenv:Body>
<ser:addService>
<ser:subscriberId>100</ser:subscriberId>
<ser:service>
<typ:name>12345678</typ:name>
<typ:deviceLimit>1</typ:deviceLimit>
<!--Zero or more repetitions:-->
<typ:subscription name="a"/>
<!--Zero or more repetitions:-->
<typ:package name="b"/>
</ser:service>
</ser:addService>
</soapenv:Body>
</soapenv:Envelope>
验证器响应此验证错误,关于subscriberId 元素:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Client</faultcode>
<faultstring>Unmarshalling Error: cvc-elt.1: Cannot find the declaration of element 'ser:subscriberId'.</faultstring>
</soap:Fault>
</soap:Body>
</soap:Envelope>
对我来说很奇怪,对于与简单长的方法调用参数相对应的生成类型之一,验证器失败了。有人提示可能是什么问题吗?
Apache CXF 版本是 2.7.10,我使用的是 Java 1.7,所以我猜它使用的是其中的任何 JAXB 实现。
【问题讨论】:
标签: java jaxb jax-ws cxf validation