Abbas Gadhia
Abbas Gadhia

Reputation: 15118

Jaxb - Overriding the XMLElement name attribute

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

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

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

Thanks

Upvotes: 3

Views: 2068

Answers (1)

Abbas Gadhia
Abbas Gadhia

Reputation: 15118

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, 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 ;)

Upvotes: 1

Related Questions