java 如何解决 wsdl2java 上 ObjectFactory 中的冲突?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25976391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:04:42  来源:igfitidea点击:

How to resolve collision in the ObjectFactory on wsdl2java?

javaxmlweb-servicesjaxbcxf

提问by membersound

I'm using CXFand wsdl2javato autogenerate webservice classes.

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

Problem: somehow the webservice I want to connect to has duplicate names for some elements:

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

Two declarations cause a collision in the ObjectFactory class

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)

[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 nodewrong here? The xsd has a xs:schematag, so why is this failing?

为什么node这里是错误的?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']评估为正确的标签。

采纳答案by membersound

It turned out I have to apply a renaming/suffix on all xs:complexTypeelements 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>

回答by lexicore

Try jaxb:factoryMethodinstead of jaxb:property.

尝试jaxb:factoryMethod代替jaxb:property.

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

An example of bindingusing jaxb:factoryMethod.

一个结合示例使用jaxb:factoryMethod

Update:

更新:

Thismight help as well.

也可能有帮助。

回答by Xstian

Property Binding Declarations

属性绑定声明

The binding declaration enables you to customize the binding of an XML schema element to its Java representation as a property. The scope of customization can either be at the definition level or component level depending upon where the binding declaration is specified.

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

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