JAXB模式到Java不同的XmlRootElement名称和类名 [英] JAXB schema to Java Different XmlRootElement name and Class name
问题描述
我有一个xsd架构,我正在从中生成一些java类。我正在使用jaxb进行生成。
I have a xsd schema from which I'm generating some java classes. I'm using jaxb for the generation.
我希望能够生成一个用 @XmlRootElement
注释的类,但我希望@XmlRootElement name属性与生成的类的名称不同。
I want to be able to generate a class annotated with @XmlRootElement
, but I want the @XmlRootElement name property to be different than the name of the generated class.
在我的xsd中我定义了以下内容:
In my xsd I'm defining the following:
<xs:element name="customer">
<xs:complexType>
<xs:sequence>
....
</xs:sequence>
</xs:complexType>
</xs:element>
这段代码生成以下java类:
This piece of code generates the following java class:
@XmlRootElement(name = "customer")
public class Customer {
...
}
@XmlRootElement
的name属性与生成的名称相同类。我希望生成的类名为 CustomerReques
t。
The name property of the @XmlRootElement
is the same as the name of the generated Class. I want the generated class name to be CustomerReques
t.
我尝试使用 jaxb:class
更改classe名称的定义。实际上,此选项会更改类名,但会删除 @XmlRootElement
注释,我需要它存在。
I've tryed to use the jaxb:class
definition to change the classe name. Indeed this option changes the class name but removes the @XmlRootElement
annotation, and I need it to be present.
以下xsd:
<xs:element name="customer">
<xs:complexType>
<xs:annotation>
<xs:appinfo>
<jaxb:class name="CustomerRequest"/>
</xs:appinfo>
</xs:annotation>
<xs:sequence>
</xs:sequence>
</xs:complexType>
</xs:element>
生成此课程:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "customer", propOrder = {
})
public class CustomerRequest {
}
如何设置 @XmlRootElement <的属性名称? / code>注释与生成的类名不同而不会丢失注释?
How can I make the property name of the @XmlRootElement
annotation different from the generated class name without loosing the annotation?
解决方案更新:
用户Xstian建议使用外部绑定的正确解决方案。
仅供进一步参考,我将使用内联绑定转换的解决方案更新我自己的帖子:
Solution update: User Xstian proposed the correct solution using external bindings. Just for further reference, I'll update my own post with the solution converted for using inline bindings:
<xs:element name="customer">
<xs:complexType>
<xs:annotation>
<xs:documentation>Request object for the operation that checks if a customer profile exists.</xs:documentation>
<xs:appinfo>
<annox:annotate>
<annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="customer"/>
</annox:annotate>
<jaxb:class name="CustomerRequest"/>
</xs:appinfo>
</xs:annotation>
<xs:sequence>
</xs:sequece>
</xs:complexType>
</xs:element>
推荐答案
我建议你使用这个绑定
<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="../your.xsd">
<bindings node="//xs:element[@name='customer']//xs:complexType">
<annox:annotate>
<annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement"
name="customer" namespace="yourNamespaceIfYouWant">
</annox:annotate>
</annox:annotate>
<class name="CustomerRequest"/>
</bindings>
</bindings>
</bindings>
Class
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"header"
})
@XmlRootElement(name = "customer", namespace = "yourNamespaceIfYouWant")
public class CustomerRequest
这篇关于JAXB模式到Java不同的XmlRootElement名称和类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!