CXF - Wsdl2java - 已定义XX属性 [英] CXF - Wsdl2java - the XX property is already defined

查看:502
本文介绍了CXF - Wsdl2java - 已定义XX属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use CXF to generate client class to access web service server. The web service are based on WCF (.NET).

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

When I call wsdl2java, I have the following error :

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选项),则不会出现此错误。

This error does not appear if I ask xmlbeans databinding (use "db xmlbeans" option).

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

Is there any way to generate classes with JAXB databinding?

推荐答案

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

This type of problem often occurs when a type has an attribute and element with the same name.

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模型时,我们会收到以下错误。

When we try to generate a Java model from this XML schema we get the following error.

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

绑定。 xml

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

A JAXB binding file can be used to customize the classes that are generated. Here we will use it to rename the property that will correspond to the bar attribute.

<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类时,你会得到一个类:

Now when you generate the Java classes you will get a class like:

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

}

这篇关于CXF - Wsdl2java - 已定义XX属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆