简体   繁体   English

如何解决wsdl2java上ObjectFactory的冲突?

[英]How to resolve collision in the ObjectFactory on wsdl2java?

I'm using CXF and wsdl2java to autogenerate webservice classes. 我正在使用CXFwsdl2java来自动生成webservice类。

Problem: somehow the webservice I want to connect to has duplicate names for some elements: 问题:我想要连接的webservice在某种程度上具有一些元素的重复名称:

Two declarations cause a collision in the ObjectFactory class

The xsd is like: xsd就像:

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

The xsd itself is imported inside the wsdl that is used to autogenerate the jaxb classes like this: xsd本身是在wsdl中导入的,用于自动生成jaxb类,如下所示:

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

I'm trying to cover this using jaxb-bindings.xml : 我试图用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>

Result: 结果:

[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)

Why is the node wrong here? 为什么这里的node错了? The xsd has a xs:schema tag, so why is this failing? xsd有一个xs:schema标签,为什么这会失败呢?

Interesting fact: when I use any xpath tool, download the XSD to my local machine an check the path, then //xs:schema/xs:element[@name='ViolatingName'] evaluates to the proper tag. 有趣的事实:当我使用任何xpath工具时,将XSD下载到我的本地机器并检查路径,然后//xs:schema/xs:element[@name='ViolatingName']评估为正确的标签。

It turned out I have to apply a renaming/suffix on all xs:complexType elements like this: 原来我必须在所有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>

Try jaxb:factoryMethod instead of jaxb:property . 尝试jaxb:factoryMethod而不是jaxb:property

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

An example of binding using jaxb:factoryMethod . 使用jaxb:factoryMethod 绑定示例 jaxb:factoryMethod

Update: 更新:

This might help as well. 也可能有所帮助。

Property Binding Declarations 属性绑定声明

The binding declaration enables you to customize the binding of an XML schema element to its Java representation as a property. 绑定声明使您可以自定义XML模式元素与其Java表示的绑定作为属性。 The scope of customization can either be at the definition level or component level depending upon where the binding declaration is specified. 自定义范围可以是定义级别,也可以是组件级别,具体取决于指定绑定声明的位置。

The syntax for customizations is: 自定义的语法是:

<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 defines the customization value propertyName; name定义自定义值propertyName; it must be a legal Java identifier. 它必须是合法的Java标识符。
  • collectionType defines the customization value propertyCollectionType, which is the collection type for the property. collectionType定义自定义值propertyCollectionType,它是属性的集合类型。 propertyCollectionType if specified, can be either indexed or any fully-qualified class name that implements java.util.List. propertyCollectionType如果指定,可以是索引或任何实现java.util.List的完全限定类名。
  • fixedAttributeAsConstantProperty defines the customization value fixedAttributeAsConstantProperty. fixedAttributeAsConstantProperty定义自定义值fixedAttributeAsConstantProperty。 The value can be either true, false, 1, or 0. 值可以是true,false,1或0。
  • generateIsSetMethod defines the customization value of generateIsSetMethod. generateIsSetMethod定义generateIsSetMethod的自定义值。 The value can be either true, false, 1, or 0. enableFailFastCheck defines the customization value enableFailFastCheck. 值可以是true,false,1或0.enableFailFastCheck定义自定义值enableFailFastCheck。 The value can be either true, false, 1, or 0. Please note that the JAXB implementation does not support failfast validation. 值可以是true,false,1或0.请注意,JAXB实现不支持failfast验证。
  • <javadoc> customizes the Javadoc tool annotations for the property's getter method. <javadoc>为属性的getter方法自定义Javadoc工具注释。

From this link 从这个链接

XSD 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>

Binding 捆绑

<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>

Generated Class 生成的类

@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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM