简体   繁体   English

更改带有JAXB注释的子类的元素名称

[英]Change element name of a JAXB annotated subclass

I am trying to create a jaxb class hierarchy for a web services domain. 我正在尝试为Web服务域创建jaxb类层次结构。 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. 我发现不方便的是,在超类中重写getter方法的子类可以更改JAXB输出的元素名称,但是超类的一个也将被写入输出中。 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>. 当我将SubClass元素添加到XmlRootElement中时,输出XML包含两个元素<Name>和<CoolName>。 Is there a way to suppress <Name> being marshaled ? 有没有办法抑制<Name>被封送?

Do you need to have the name property on SuperClass mapped? 您是否需要在SuperClass上映射name属性? If not you could just mark that property @XmlTransient. 如果没有,您可以标记该属性@XmlTransient。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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