【问题标题】:How to resolve collision in the ObjectFactory on wsdl2java?如何解决 wsdl2java 上的 ObjectFactory 中的冲突?
【发布时间】:2014-11-16 13:20:02
【问题描述】:

我正在使用CXFwsdl2java 来自动生成网络服务类。

问题:不知何故,我想连接的网络服务有一些元素的重复名称:

Two declarations cause a collision in the ObjectFactory class

xsd 是这样的:

<xs:schema targetNamespace="http://thenamespace">
    <xs:complexType name="ViolatingName">
     ...
    </xs:complexType>
    <xs:element name="ViolatingName" nillable="true" type="tns:ViolatingName"/>
</xs:schema>

xsd 本身被导入到用于自动生成 jaxb 类的 wsdl 中,如下所示:

<wsdl:types>
    <xsd:schema targetNamespace="http://imports">
        <xsd:import schemaLocation="https://path.to.xsd" namespace="http://thenamespace" />

我正在尝试使用jaxb-bindings.xml

<jaxb:bindings    
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    jaxb:extensionBindingPrefixes="xjc"   
    jaxb:version="2.1">

    <jaxb:bindings schemalocation="https://path.to.xsd" node="//xs:schema">
    <jaxb:bindings node=".//xs:element[@name='ViolatingName']">
            <jaxb:property name="ViolatingNameBinding" />
        </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>

结果:

[ERROR] XPath evaluation of "//xs:schema" results in empty target node (org.apache.cxf:cxf-codegen-plugin:3.0.1:wsdl2java:generate-sources:generate-sources)

为什么node 在这里错了? xsd 有一个xs:schema 标签,那为什么会失败呢?

有趣的事实:当我使用任何 xpath 工具时,将 XSD 下载到我的本地计算机并检查路径,然后 //xs:schema/xs:element[@name='ViolatingName'] 评估为正确的标记。

【问题讨论】:

  • 在标签绑定的 xs:schema 上缺少双 '//'..
  • 我在上面也做了调整,但结果是一样的...

标签: java xml web-services jaxb cxf


【解决方案1】:

事实证明,我必须对所有 xs:complexType 元素应用重命名/后缀,如下所示:

<jaxb:bindings schemaLocation="https://path.to.xsd" node="/xs:schema">
    <jaxb:schemaBindings>
        <jaxb:nameXmlTransform>
            <jaxb:typeName suffix="Type" />
        </jaxb:nameXmlTransform>
    </jaxb:schemaBindings>
</jaxb:bindings>

【讨论】:

    【解决方案2】:

    尝试jaxb:factoryMethod 而不是jaxb:property

    <jaxb:bindings node=".//xs:element[@name='ViolatingName']">
        <jaxb:factoryMethod name="ViolatingName1" />
    </jaxb:bindings>
    

    使用jaxb:factoryMethodexample of binding

    更新:

    This 可能也会有所帮助。

    【讨论】:

    • 哪一个?您已经列出了两个 - 一个带有碰撞,一个带有空目标节点。
    • 对不起,我输入了空的目标节点。没有显示共谋,因为 wsdl2java 根本没有达到可能发生此异常的地步。,
    • 请查看更新。我猜你正在编译一个 WSDL,所以 wsdl2java 可能需要一些特殊的带有绑定的 mumbo-jumbo。请看最后一个链接:stackoverflow.com/questions/22425306/…
    【解决方案3】:

    属性绑定声明

    绑定声明使您能够自定义 XML 模式元素与其 Java 表示的绑定作为属性。自定义范围可以在定义级别或组件级别,具体取决于指定绑定声明的位置。

    自定义的语法是:

    <property      [ name = "propertyName"]
      [ collectionType = "propertyCollectionType" ]
      [ fixedAttributeAsConstantProperty = "true" | "false" | "1" | "0" ]
      [ generateIsSetMethod = "true" | "false" | "1" | "0" ]
      [ enableFailFastCheck ="true" | "false" | "1" | "0" ]
      [ <baseType> ... </baseType> ]
      [ <javadoc> ... </javadoc> ]
    </property>
    
    <baseType>
      <javaType> ... </javaType>
    </baseType> 
    
    • name定义自定义值propertyName;它必须是合法的 Java 标识符。
    • collectionType 定义自定义值propertyCollectionType,它是属性的集合类型。 propertyCollectionType(如果指定)可以是索引或任何实现 java.util.List 的完全限定类名。
    • fixedAttributeAsConstantProperty 定义自定义值 fixedAttributeAsConstantProperty。该值可以是 true、false、1 或 0。
    • generateIsSetMethod 定义了 generateIsSetMethod 的自定义值。该值可以是 true、false、1 或 0。 enableFailFastCheck 定义自定义值 enableFailFastCheck。该值可以是 true、false、1 或 0。请注意,JAXB 实现不支持故障快速验证。
    • &lt;javadoc&gt; 为属性的 getter 方法自定义 Javadoc 工具注解。

    来自link

    XSD

    <xs:schema targetNamespace="http://thenamespace">
        <xs:element name="ViolatingName" type="tns:ViolatingName"/> 
        <xs:complexType name="ViolatingName">
            <xs:all>
                <xs:element name="prova" type="xs:string"/>
            </xs:all>
        </xs:complexType>
    
        <xs:element name="AdditionalInfos" type="AdditionalInfos"/>
        <xs:complexType name="AdditionalInfos">
            <xs:sequence>
                <xs:element minOccurs="1" name="ViolatingName" type="tns:ViolatingName"/>
            </xs:sequence>
        </xs:complexType>
    </xs:schema>
    

    绑定

    <bindings version="2.0" xmlns="http://java.sun.com/xml/ns/jaxb"
        xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
        xmlns:annox="http://annox.dev.java.net"
        xmlns:namespace="http://jaxb2-commons.dev.java.net/namespace-prefix">
        <bindings schemaLocation="../path/of/your.xsd">
    
                <bindings node="//xs:complexType[@name='AdditionalInfos']//xs:sequence//xs:element[@name='ViolatingName']">
                    <property name="aaa" />
                </bindings>
    
        </bindings>
    </bindings>
    

    生成的类

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "AdditionalInfos", propOrder = {
        "aaa",
    })
    @XmlRootElement
    public class AdditionalInfos
        implements Serializable
    {
    
        private final static long serialVersionUID = 12343L;
        @XmlElement(name = "ViolatingName", required = true)
        protected ViolatingName aaa;
    

    【讨论】:

      猜你喜欢
      • 2015-04-27
      • 1970-01-01
      • 2020-09-15
      • 2014-04-20
      • 1970-01-01
      • 2011-05-12
      • 2018-05-15
      • 2017-08-13
      • 2013-09-13
      相关资源
      最近更新 更多