覆盖XMLElement名称属性

覆盖XMLElement名称属性

本文介绍了Jaxb - 覆盖XMLElement名称属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个父类ClassA,我无法修改。它有一个字段field1,上面没有XmlElement注释。

I have a parent class ClassA, that I cannot modify. It has a field "field1" which does not have a XmlElement annotation on it.


@XmlRootElement
public class ClassA {
    protected String field1;
}

另外,我有一个扩展ClassA的ClassB类。我收到的xml,字段field1,名为newField1。

Also, I have a class ClassB that extends ClassA. The xml that i recieve, has the field "field1" named as "newField1".

我如何向Jaxb表明xml中的标签newField1实际上对应于基类中的field1。

How do i go about indicating to Jaxb that the tag "newField1" in the xml actually corresponds to "field1" in the base class.

谢谢

推荐答案

确定。找到了答案。
所有需要做的就是在派生类中创建一个setter方法,例如

Ok. Found the answer.All that needs to be done is to create a setter method in the derived class such as


    @XmlElement(name = "newField1")
    public void setField1(String field1) {
        this.field1 = field1;
    }

Ofc,这假设您正在访问的字段具有受保护的访问权限基类。否则你可能需要稍微肮脏并做一些反思;)

Ofc, this assumes that the field that you are accessing has protected access in the base class. Else you might need to get a little dirty and do some reflection ;)

这篇关于Jaxb - 覆盖XMLElement名称属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 11:03