【问题标题】:WS Client generation using maven and reusing classes使用 maven 和重用类的 WS 客户端生成
【发布时间】:2015-04-05 02:08:44
【问题描述】:

我的应用程序分为两层,后端层暴露了一堆 Web 服务,而表现层使用这些服务来获取业务数据。

为了使我的构建尽可能自动化,我将所有生成的 WSDL 和关联的 XSD 文件打包成一个 ZIP 依赖​​项(使用 Maven 汇编程序插件),它会在安装阶段上传到我的 nexus。

然后在 UI POM 上,我将这个 ZIP 文件(使用 maven 依赖插件)解压到本地目录(src/main/resources/wsdl),然后使用 CXF-CODEGEN 插件生成客户端。

问题是——其中许多服务共享公共模型实体,例如,许多方法使用Catalogue 类,这是我用于由 Id 和文本值组成的对象的实体。

使用 CXF 生成的代码,我最终会为我拥有的每个 Web 服务创建一个不同的 Catalogue 类(具有不同的包名称),因此在我的代码中失去了 OO 多态功能,因为它们在实践中都是不同的类。

我听说您可以通过某种方式首先将 XSD 类编译成称为“episodes”的东西,然后将这些 episode 文件提供给 CXF 以告诉它利用它们而不是生成类来避免这个问题。

我尝试添加一个额外的编译步骤,在该步骤中我运行 maven-jaxb2-plugin 从 XSD 生成类,但我得到了 org.xml.sax.SAXParseException; (...) 'catalogueDTO' is already defined 异常,因为此类在 XSD 文件中重复。

谁能指出我正确的方向?

【问题讨论】:

  • 您的catalogue 类是定义在一个 XSD 文件还是多个 XSD 文件中?
  • 几个,基本上每个wsdl一个
  • 那是你的问题。很快就会回复。
  • 看我的回答我怀疑这是你想要达到的目标。

标签: xml web-services maven xsd cxf-codegen-plugin


【解决方案1】:

如果您的 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>

或者,如果您想要视觉表示:

厨房 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。

让我知道这是否有意义。

【讨论】:

  • 哇,这是一个彻底的答案......感谢您的巨大努力!这确实是有道理的,如果我正确理解我所面临的问题是我的对象是在每个 WSDL 中声明的,具有不同的名称空间......我检查了它们,确实是这样。我确实可以控制我的 Web 服务,所以我可以更改它,前提是 CXF(这是我用来创建我的 WS)允许我显式声明我的对象的命名空间。
  • 好吧,我建议你去试试。我相信你会有更多的问题。请记住,在执行合约优先/自上而下的方法时,必须妥善维护命名空间。这些概念是 SOA 治理的一部分,对于 SOA 应用程序获得 SOA 的好处至关重要。
  • 谢谢,您确实为我指明了正确的方向,因为我是 SOAP Web 服务和所有的新手 :)
猜你喜欢
  • 1970-01-01
  • 2015-05-23
  • 2015-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多