【发布时间】:2014-10-24 08:57:39
【问题描述】:
我有一个 xsd 模式,我从中生成一些 java 类。我正在使用 jaxb 生成。
我希望能够生成一个使用@XmlRootElement 注释的类,但我希望@XmlRootElement 名称属性与生成的类的名称不同。
在我的 xsd 中,我定义了以下内容:
<xs:element name="customer">
<xs:complexType>
<xs:sequence>
....
</xs:sequence>
</xs:complexType>
</xs:element>
这段代码生成如下java类:
@XmlRootElement(name = "customer")
public class Customer {
...
}
@XmlRootElement 的 name 属性与生成的 Class 的名称相同。我希望生成的类名是CustomerRequest。
我尝试使用jaxb:class 定义来更改类名。事实上,这个选项改变了类名,但删除了 @XmlRootElement 注释,我需要它存在。
以下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注解的属性名与生成的类名不同而不丢失注解?
解决方案更新: 用户 Xstian 提出了使用外部绑定的正确解决方案。 仅供参考,我将使用转换为使用内联绑定的解决方案更新我自己的帖子:
<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>
【问题讨论】:
-
您现在可以在 JAXB2 Annotate 插件中使用 Java 语法。
@javax.xml.bind.annotation.XmlRootElement( name="customer", namespace="yourNamespaceIfYouWant") 之类的东西应该代替内部的annox:annotate元素起作用。 (我是作者。)
标签: jaxb xsd jaxb2-basics annox jaxb2-annotate-plugin