【问题标题】:CXF - Wsdl2java - the XX property is already definedCXF - Wsdl2java - XX 属性已定义
【发布时间】:2013-05-18 20:16:19
【问题描述】:

我使用 CXF 生成客户端类来访问 Web 服务服务器。 Web 服务基于 WCF (.NET)。

当我调用 wsdl2java 时,出现以下错误:

The id property is already defined. use <jaxb:property> to resolve this conflict. The following location matches the above error : http://*****/WcfDemandService.svc?xsd=xsd2 [0,0]

如果我询问 xmlbeans 数据绑定(使用“db xmlbeans”选项),则不会出现此错误。

有没有办法使用 JAXB 数据绑定生成类?

【问题讨论】:

    标签: properties jaxb cxf wsdl2java defined


    【解决方案1】:

    当一个类型有同名的属性和元素时,经常会出现这种类型的问题。

    schema.xsd

    <?xml version="1.0" encoding="UTF-8"?>
    <schema 
        xmlns="http://www.w3.org/2001/XMLSchema" 
        targetNamespace="http://www.example.org/schema" 
        xmlns:tns="http://www.example.org/schema" 
        elementFormDefault="qualified">
    
        <element name="foo">
            <complexType>
                <sequence>
                    <element name="bar" type="string"/>
                </sequence>
                <attribute name="bar" type="string"/>
            </complexType>
        </element>
    
    </schema>
    

    xjc schema.xsd

    当我们尝试从这个 XML 模式生成 Java 模型时,我们会收到以下错误。

    parsing a schema...
    [ERROR] Property "Bar" is already defined. Use &lt;jaxb:property> to resolve this conflict.
      line 11 of file:/Users/bdoughan/Scratch/src/forum16714465/schema.xsd
    
    [ERROR] The following location is relevant to the above error
      line 13 of file:/Users/bdoughan/Scratch/src/forum16714465/schema.xsd
    

    binding.xml

    JAXB 绑定文件可用于自定义生成的类。在这里,我们将使用它来重命名与bar 属性对应的属性。

    <jxb:bindings 
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
        version="2.1">
    
        <jxb:bindings schemaLocation="schema.xsd">
                <jxb:bindings node="//xs:element[@name='foo']/xs:complexType/xs:attribute[@name='bar']">
                    <jxb:property name="barAttribute"/>
                </jxb:bindings>
        </jxb:bindings>
    
    </jxb:bindings>
    

    xjc -b binding.xml schema.xsd

    现在,当您生成 Java 类时,您将获得如下类:

    package org.example.schema;
    
    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlElement;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "bar"
    })
    @XmlRootElement(name = "foo")
    public class Foo {
    
        @XmlElement(required = true)
        protected String bar;
        @XmlAttribute(name = "bar")
        protected String barAttribute;
    
        public String getBar() {
            return bar;
        }
    
        public void setBar(String value) {
            this.bar = value;
        }
    
        public String getBarAttribute() {
            return barAttribute;
        }
    
        public void setBarAttribute(String value) {
            this.barAttribute = value;
        }
    
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-24
    • 2013-12-05
    • 2010-11-16
    • 2018-03-22
    • 2012-12-24
    • 2019-04-26
    • 2013-05-31
    相关资源
    最近更新 更多