CXF - Wsdl2java - 已定义XX属性

时间:2013-05-23 12:52:10

标签: properties jaxb cxf wsdl2java defined

我使用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数据绑定生成类?

1 个答案:

答案 0 :(得分:6)

当类型具有属性和具有相同名称的元素时,通常会发生此类问题。

<强> 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;
    }

}