当您的 Web 服务中有一些将跨服务和操作使用的基类时,最好在单个 XSD 中声明这些类,然后在您的项目中包含并使用此 XSD。
以下示例将向您展示如何操作。我将这个示例基于提供的信息,因此它可能不完全适合您的需要,但会向您展示这些概念。
首先让我们创建一个 Catalog.xsd 文件并使用以下代码在此文件中声明我们的目录对象:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns="http://www.yourcompany.com/Services_V1/CommonType"
elementFormDefault="qualified"
targetNamespace="http://www.yourcompany.com/Services_V1/CommonType"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="CatalogueID_Type">
<xs:annotation>
<xs:documentation>The Catalogue ID is an integer value that is used to uniquely identify the catalog item on the database.</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:int" />
</xs:simpleType>
<xs:simpleType name="CatalogueValue_Type">
<xs:annotation>
<xs:documentation>The catalog is a string value that will contain the information describing the catalog key.</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:string" />
</xs:simpleType>
<xs:complexType name="Catalogue_Type">
<xs:sequence>
<xs:element ref="CatalogID"
minOccurs="1"
maxOccurs="1" />
<xs:element ref="CatalogueValue"
minOccurs="0"
maxOccurs="1" />
</xs:sequence>
</xs:complexType>
<xs:element name="CatalogID"
type="CatalogueID_Type" />
<xs:element name="CatalogueValue"
type="CatalogueValue_Type" />
<xs:element name="Catalogue"
type="Catalogue_Type" />
或者,如果您喜欢视觉表示:
需要注意的是,目录类/对象有一个命名空间,http://www.yourcompany.com/Services_V1/CommonType
我们将在 WSDL 中再次使用这个命名空间。这个对象现在将被两个服务使用,BathroomCatalogue Service 和 KicthenCatalogue Service。为简单起见,我在服务中只有一个称为 GetCatalogueItem 的操作。对于这些服务中的每一项,我将包含该catalog.xsd
文件并重用此目录对象。
这是浴室服务的 WSDL:
<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions name="BathroomCatalogueSRV"
targetNamespace="http://www.yourcompany.com/Services_V1/BathroomCatalogueService"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
xmlns:tns="http://www.yourcompany.com/Services_V1/BathroomCatalogueService"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:catalogue="http://www.yourcompany.com/Services_V1/CommonType">
<wsdl:types>
<xs:schema elementFormDefault="qualified"
targetNamespace="http://example.com/">
<xs:import schemaLocation="Catalog.xsd"
namespace="http://www.yourcompany.com/Services_V1/CommonType" />
</xs:schema>
</wsdl:types>
<wsdl:message name="GetCatalogueItemReq">
<wsdl:part name="GetCatalogueItemReq"
element="catalogue:Catalogue" />
</wsdl:message>
<wsdl:message name="GetCatalogueItemRs">
<wsdl:part name="GetCatalogueItemRs"
element="catalogue:Catalogue" />
</wsdl:message>
<wsdl:portType name="BathroomCataloguePortType">
<wsdl:operation name="GetCatalogueItem">
<wsdl:input message="tns:GetCatalogueItemReq" />
<wsdl:output message="tns:GetCatalogueItemRs" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="BathroomCatalogueBinding"
type="tns:BathroomCataloguePortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"
style="document" />
<wsdl:operation name="GetCatalogueItem">
<wsdl:input />
<wsdl:output />
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="BathroomCatalogueService">
<wsdl:port name="BathroomCataloguePort"
binding="tns:BathroomCatalogueBinding">
<soap:address location="http://www.yourcompany.com/Services_V1/BathroomCatalogueService" />
</wsdl:port>
</wsdl:service>
或者,如果您想要视觉表示:
Kitchen WSDL 如下所示:
<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions name="KitchenCatalogueSRV"
targetNamespace="http://www.yourcompany.com/Services_V1/KitchenCatalogueService"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
xmlns:tns="http://www.yourcompany.com/Services_V1/KitchenCatalogueService"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:catalogue="http://www.yourcompany.com/Services_V1/CommonType">
<wsdl:types>
<xs:schema elementFormDefault="qualified"
targetNamespace="http://example.com/">
<xs:import schemaLocation="Catalog.xsd"
namespace="http://www.yourcompany.com/Services_V1/CommonType" />
</xs:schema>
</wsdl:types>
<wsdl:message name="GetCatalogueItemReq">
<wsdl:part name="GetCatalogueItemReq"
element="catalogue:Catalogue" />
</wsdl:message>
<wsdl:message name="GetCatalogueItemRs">
<wsdl:part name="GetCatalogueItemRs"
element="catalogue:Catalogue" />
</wsdl:message>
<wsdl:portType name="KitchenCataloguePortType">
<wsdl:operation name="GetCatalogueItem">
<wsdl:input message="tns:GetCatalogueItemReq" />
<wsdl:output message="tns:GetCatalogueItemRs" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="KitchenCatalogueBinding"
type="tns:KitchenCataloguePortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"
style="document" />
<wsdl:operation name="GetCatalogueItem">
<wsdl:input />
<wsdl:output />
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="KitchenCatalogueService">
<wsdl:port name="KitchenCataloguePort"
binding="tns:KitchenCatalogueBinding">
<soap:address location="http://www.yourcompany.com/Services_V1/KitchenCatalogueService" />
</wsdl:port>
</wsdl:service>
再一次在视觉上:
在这两个 WSDL 文件中,我都包含了 catalog.xsd 文件。您可以在以下代码行中看到这一点:
<xs:import schemaLocation="Catalog.xsd"
namespace="http://www.yourcompany.com/Services_V1/CommonType" />
现在,当我将这些 WSDL 和 XSD 与 cxf 一起使用时,两个服务将只使用一个目录对象/类。我很快做了一个shell项目,生成了以下结构:
请注意,我声明的 CatalogueType 现在与我声明的命名空间位于一个包中。
查看服务类时,浴室和厨房服务都将使用这一类。在它使用的厨房服务类中com.yourcompany.services_v1.commontype.CatalogueType
。
@WebService(targetNamespace = "http://www.yourcompany.com/Services_V1/KitchenCatalogueService", name = "KitchenCataloguePortType")
@XmlSeeAlso({com.yourcompany.services_v1.commontype.ObjectFactory.class})
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
public interface KitchenCataloguePortType {
@WebResult(name = "Catalogue", targetNamespace = "http://www.yourcompany.com/Services_V1/CommonType", partName = "GetCatalogueItemRs")
@WebMethod(operationName = "GetCatalogueItem")
public com.yourcompany.services_v1.commontype.CatalogueType getCatalogueItem(
@WebParam(partName = "GetCatalogueItemReq", name = "Catalogue", targetNamespace = "http://www.yourcompany.com/Services_V1/CommonType")
com.yourcompany.services_v1.commontype.CatalogueType getCatalogueItemReq
);
}
并且在它使用的厨房服务类中com.yourcompany.services_v1.commontype.CatalogueType
。
@WebService(targetNamespace = "http://www.yourcompany.com/Services_V1/BathroomCatalogueService", name = "BathroomCataloguePortType")
@XmlSeeAlso({com.yourcompany.services_v1.commontype.ObjectFactory.class})
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
public interface BathroomCataloguePortType {
@WebResult(name = "Catalogue", targetNamespace = "http://www.yourcompany.com/Services_V1/CommonType", partName = "GetCatalogueItemRs")
@WebMethod(operationName = "GetCatalogueItem")
public com.yourcompany.services_v1.commontype.CatalogueType getCatalogueItem(
@WebParam(partName = "GetCatalogueItemReq", name = "Catalogue", targetNamespace = "http://www.yourcompany.com/Services_V1/CommonType")
com.yourcompany.services_v1.commontype.CatalogueType getCatalogueItemReq
);
}
只是提醒您千万不要编辑这些类,因为它们是由 CXF 派生和生成的,您的更改将丢失。因此,通过首先执行 WSDL 方法,您必须确保正确构建 XSD 文件和 WSDL。
让我知道这是否有意义。