简体   繁体   中英

Change element name of a JAXB annotated subclass

I am trying to create a jaxb class hierarchy for a web services domain. I find it inconvenient that a subclass that overrides a getter method in the super class can change the element name that JAXB outputs, but the super class's one is also being written to the output. I am wondering if there is a way to suppress the getter in the super class.

Code:

@XmlType
class SuperClass
{
    @XmlElement(name = "Name")
    public String getName(){}
}

@XmlType
class SubClass extends SuperClass
{
    @Override
    @XmlElement(name = "CoolName")
    public String getName(){}
}

When I add the SubClass element into an XmlRootElement, the output XML contains two elements, <Name> and <CoolName>. Is there a way to suppress <Name> being marshaled ?

Do you need to have the name property on SuperClass mapped? If not you could just mark that property @XmlTransient.

class SuperClass
{
    @XmlTransient
    public String getName(){}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM