1

I have a three-tier inheritance structure being generated by JAXB: There's the basic User class, a UserProfile class that extends User, and a UserSystem class that extends UserProfile. I need both UserProfile and UserSystem to have the @XmlRootElement annotation so they can be marshalled. However, the schema below results in only UserSystem having @XmlRootElement.

To my knowledge, I have followed the solution in this question.

Here are the relevant parts of my schema:

<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>

The result is that UserSystem has the correct annotation:

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

But UserProfile does not:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "UserProfile", propOrder = {
    "permissions",
    "roles"
})
@XmlSeeAlso({
    UserSystem.class
})
public class UserProfile
    extends User
    implements Serializable

I also have the extension="true" parameter in my Ant build script:

<target name="messages" depends="clean" description="Create java binding java files">
    <echo message="Compiling the schema..." />
    <xjc schema="${common.loc}/WebContent/master.xsd" package="com.l3com.apps.messages" destdir="${common.loc}/src" extension="true" />
</target>

I am using JAXB RI 2.2.11.

Stacy Read
  • 11
  • 3
  • Your question says `@XmlRootElement` is missing,but title says `@XmlRootAttribute` was not generated. Did you mean `@XmlRootElement` in title? – Thomas Fritsch Jun 24 '17 at 21:24
  • @ThomasFritsch I certainly did. (Proofread everything except the title, apparently.) Sorry for the late reply; my inbox notifications were turned off. – Stacy Read Jul 27 '17 at 20:52

0 Answers0