[英]JAXB schema to Java Different XmlRootElement name and Class name
I have a xsd schema from which I'm generating some java classes. 我有一个xsd架构,我正在从中生成一些java类。 I'm using jaxb for the generation. 我正在使用jaxb代代相传。
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. 我希望能够生成一个使用@XmlRootElement
注释的类,但我希望@XmlRootElement name属性与生成的类的名称不同。
In my xsd I'm defining the following: 在我的xsd中,我定义了以下内容:
<xs:element name="customer">
<xs:complexType>
<xs:sequence>
....
</xs:sequence>
</xs:complexType>
</xs:element>
This piece of code generates the following java class: 这段代码生成以下java类:
@XmlRootElement(name = "customer")
public class Customer {
...
}
The name property of the @XmlRootElement
is the same as the name of the generated Class. @XmlRootElement
的name属性与生成的Class的名称相同。 I want the generated class name to be CustomerReques
t. 我希望生成的类名是CustomerReques
t。
I've tryed to use the jaxb:class
definition to change the classe name. 我已经尝试使用jaxb:class
定义来更改classe名称。 Indeed this option changes the class name but removes the @XmlRootElement
annotation, and I need it to be present. 实际上,此选项会更改类名,但会删除@XmlRootElement
注释,我需要它存在。
The following xsd: 以下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>
Generates this class: 生成这个类:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "customer", propOrder = {
})
public class CustomerRequest {
}
How can I make the property name of the @XmlRootElement
annotation different from the generated class name without loosing the annotation? 如何在不丢失注释的情况下使@XmlRootElement
注释的属性名与生成的类名不同?
Solution update: User Xstian proposed the correct solution using external bindings. 解决方案更新:用户Xstian使用外部绑定提出了正确的解决方案。 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>
I suggest you to use this bindings 我建议你使用这个绑定
<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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.