Java JAXB - 属性“值”已定义。使用 <jaxb:property> 解决这个冲突
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4394134/
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
JAXB - Property "Value" is already defined. Use <jaxb:property> to resolve this conflict
提问by brasskazoo
Using JAXB to generate XML binding classes.
使用 JAXB 生成 XML 绑定类。
The schema is based on a set of legacy XML files, and includes this snippet:
该架构基于一组遗留 XML 文件,并包含以下代码段:
<xs:complexType name="MetaType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="Name" />
<xs:attribute type="xs:string" name="Scheme" />
<xs:attribute type="xs:string" name="Value" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
The 'Value' attribute conflicts with the 'value' property of xs:string
, and the code generation fails with the error:
'Value' 属性与 'value' 属性冲突,xs:string
代码生成失败并显示错误:
com.sun.istack.SAXParseException2: Property "Value" is already defined. Use <jaxb:property> to resolve this conflict.
采纳答案by brasskazoo
The answer lies in making use of JAXB bindings (site-template.xjb
):
答案在于使用 JAXB 绑定 ( site-template.xjb
):
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="2.1">
<bindings schemaLocation="site-template.xsd" version="1.0">
<!-- Customise the package name -->
<schemaBindings>
<package name="com.example.schema"/>
</schemaBindings>
<!-- rename the value element -->
<bindings node="//xs:complexType[@name='MetaType']">
<bindings node=".//xs:attribute[@name='Value']">
<property name="ValueAttribute"/>
</bindings>
</bindings>
</bindings>
</bindings>
The XPath expressions locate the nodes and renames it, thereby avoiding the naming conflict.
XPath 表达式定位节点并重命名它,从而避免命名冲突。
Using this bindings XML file, the generated Java class ends up having the desired getValueAttribute()
(as well as the getValue()
).
使用此绑定 XML 文件,生成的 Java 类最终具有所需的getValueAttribute()
(以及getValue()
)。
回答by user1787681
I had a problem using the solution with Eclipse (tried both Helios SR1 and Juno SR1) and CXF 2.6.3. The solution was similar to what Kaitsu says. Namely the New > Web Service wizard of Eclipse copies the wsdl into the foldre WebContent/wsdl. I had to place the wsdl and the binding file there myself. Otherwise the binding file gave the "is not a part of this compilation" error.
我在使用 Eclipse(尝试了 Helios SR1 和 Juno SR1)和 CXF 2.6.3 的解决方案时遇到了问题。解决方案类似于Kaitsu所说的。即Eclipse 的New > Web Service 向导将wsdl 复制到文件夹WebContent/wsdl 中。我必须自己将 wsdl 和绑定文件放在那里。否则绑定文件会给出“不是此编译的一部分”错误。
I wasn't able to use an inline schema in the WSDL but it did work with an external schema like in answer #1.
我无法在 WSDL 中使用内联模式,但它确实可以与答案 #1 中的外部模式一起使用。
I'm using the CXF Servlet endpoint config option. In my WSDL I have:
我正在使用 CXF Servlet 端点配置选项。在我的 WSDL 中,我有:
<wsdl:port binding="axis2:ConverterSOAP12Binding" name="ConverterSOAP12port_http">
<soap12:address location="http://localhost/Converter/services/Converter"/>
</wsdl:port>
The wizard generated this into my web.xml, which works ok:
向导将其生成到我的 web.xml 中,它可以正常工作:
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
But it put this into cxf-servlet.xml:
但它把它放到 cxf-servlet.xml 中:
<jaxws:endpoint xmlns:tns="http://wtp" id="converterporttype"
implementor="wtp.ConverterPortTypeImpl" wsdlLocation="wsdl/Converter.wsdl"
endpointName="tns:ConverterSOAP12port_http" serviceName="tns:Converter"
address="/ConverterSOAP12port_http">
<jaxws:features>
<bean class="org.apache.cxf.feature.LoggingFeature" />
</jaxws:features>
</jaxws:endpoint>
I had to change the address into the full URL, like this:
我不得不将地址更改为完整的 URL,如下所示:
address="http://localhost:8080/Converter/services/Converter">
回答by Constantino Cronemberger
This bindings file mentioned in the other answer did not work for me with CXF 3.0.0. Notice that jaxb namespace has an element "bindings" and so do the namespace jaxws, so we need to declare them:
另一个答案中提到的绑定文件不适用于 CXF 3.0.0。注意 jaxb 命名空间有一个元素“bindings”,命名空间 jaxws 也是如此,所以我们需要声明它们:
<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns="http://java.sun.com/xml/ns/jaxws"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
wsdlLocation="mesh.wsdl" >
<bindings node="wsdl:definitions/wsdl:types/xs:schema[...">
<jaxb:bindings node="./xs:element[@name='Profiles']">
<jaxb:property name="ProfilesElement"/>
</jaxb:bindings>
</bindings>
</bindings>
In my case the schema was already inside the WSDL so I did no have to specify the schemaLocation attribute.
在我的例子中,模式已经在 WSDL 中,所以我不必指定 schemaLocation 属性。
回答by rrayasam
Once after xxxx.xjb file is created for duplicate attribute name "value" (duplicate is default 'value' provided by JAXB) as below, run XJC command to create JAXB objects
一旦为重复的属性名称“值”(重复是 JAXB 提供的默认“值”)创建 xxxx.xjb 文件后,如下所示,运行 XJC 命令以创建 JAXB 对象
xjc -p "com.track.doc" -d "C:\JAXBDocuments\prasam\Desktop\JAXB_me\DealerTrace" appSamp.xsd -b xxxx.xjb
xjc -p "com.track.doc" -d "C:\JAXBDocuments\prasam\Desktop\JAXB_me\DealerTrace" appSamp.xsd -b xxxx.xjb
appSmp.xsd:-
appSmp.xsd:-
<xsd:complexType name="range">
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="value" type="xsd:string"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
xxxx.xjb:-
xxxx.xjb:-
<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="2.1">
<bindings schemaLocation="appSmp.xsd" version="1.0">
<schemaBindings>
<package name="com.track.doc"/>
</schemaBindings>
<bindings node="//xs:complexType[@name='range']">
<bindings node=".//xs:attribute[@name='value']">
<property name="valueAttribute"/>
</bindings>
</bindings>
</bindings>
</bindings>
回答by Gary
If you want to avoid creating/changing a JAXB bindings file, and you don't mind annotating your XSD, you can add the jxb:propertyannotation to your attribute's definition, e.g.:
如果您想避免创建/更改 JAXB 绑定文件,并且不介意对 XSD 进行注释,则可以将jxb:property注释添加到您的属性定义中,例如:
<xs:complexType name="MetaType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="Name" />
<xs:attribute type="xs:string" name="Scheme" />
<xs:attribute type="xs:string" name="Value">
<!-- rename property generated by JAXB (avoiding "Value" name conflict) -->
<xs:annotation>
<xs:appinfo>
<jxb:property name="valueAttribute"/>
</xs:appinfo>
</xs:annotation>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
with suitable additions to the xs:schema tag:
对 xs:schema 标签进行适当的添加:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
jxb:version="2.1">
回答by Hernan Ramovecchi
you can also use the parameter -XautoNameResolution in the command line and also in the pluggin to let jxc resolve the name if you don′t bother about the name on the classes.
如果您不关心类上的名称,您还可以在命令行和插件中使用参数 -XautoNameResolution 来让 jxc 解析名称。
回答by Ricardo álvarez
None of this bindings worked for me, i got this error:
这些绑定都不适合我,我收到了这个错误:
[ERROR] La evaluación de XPath de ".//xs:attribute[@name='Value']" produce un nodo de destino vacío
It produced an empty target node... Then i realized (after 30 minutes of dispair) that my binding was aiming to a complexType instead of an element. The answer was in my xsd file.
它产生了一个空的目标节点......然后我意识到(在 30 分钟的失望之后)我的绑定是针对 complexType 而不是一个元素。答案在我的 xsd 文件中。
Thank you
谢谢