当一个类由另一个类

时间:2017-06-23 18:51:20

标签: java xsd jaxb xjc

我有一个由JAXB生成的三层继承结构:基本的User类,UserProfile类扩展User,以及{{1扩展UserSystem的类。我需要UserProfileUserProfile都有UserSystem注释,以便可以对它们进行编组。但是,下面的架构只会导致@XmlRootElement UserSystem

据我所知,我已经按照this question中的解决方案。

以下是我的架构的相关部分:

@XmlRootElement

结果是<xsd:schema xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xsd="http://www.w3.org/2001/XMLSchema" jaxb:version="2.1" jaxb:extensionBindingPrefixes="xjc"> <xsd:annotation> <xsd:appinfo> <jaxb:globalBindings generateIsSetMethod="true" typesafeEnumBase="xsd:string"> <xjc:serializable uid="1"/> <xjc:superClass name="com.mycompany.common.BaseClass"/> <xjc:simple/> </jaxb:globalBindings> </xsd:appinfo> </xsd:annotation> <xsd:complexType name="User"> <xsd:attribute name="id" type="xsd:int" use="optional" default="0"/> <xsd:attribute name="firstname" type="xsd:string" use="required"/> <xsd:attribute name="lastname" type="xsd:string" use="required"/> </xsd:complexType> <xsd:element name="UserProfile" type="UserProfile"/> <xsd:complexType name="UserProfile"> <xsd:complexContent> <xsd:extension base="User"> <xsd:sequence> <xsd:element name="permissions" type="xsd:int" minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="role" type="xsd:int" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> <xsd:element name="UserSystem" type="UserSystem"/> <xsd:complexType name="UserSystem"> <xsd:complexContent> <xsd:extension base="UserProfile"> <xsd:attribute name="username" type="xsd:string" use="required"/> <xsd:attribute name="lastLoginDate" type="xsd:long" use="optional" default="0"/> <xsd:attribute name="locked" type="xsd:boolean" use="required"/> </xsd:extension> </xsd:complexContent> </xsd:complexType> 具有正确的注释:

UserSystem

@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "UserSystem") @XmlRootElement(name = "UserSystem") public class UserSystem extends UserProfile implements Serializable 没有:

UserProfile

我的Ant构建脚本中也有@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "UserProfile", propOrder = { "permissions", "roles" }) @XmlSeeAlso({ UserSystem.class }) public class UserProfile extends User implements Serializable 参数:

extension="true"

我正在使用JAXB RI 2.2.11。

0 个答案:

没有答案