Search code examples
javajaxbjaxb2

XmlRootElement annotation at abstract class - possible?


This is what I'm trying to do:

@XmlRootElement(name = "bar")
@XmlAccessorType(XmlAccessType.NONE)
public abstract class Bar {
}

public final class Foo extends Bar {
    @XmlElement
    public String getMsg() {
        return "hello, world!";
    }
}

Now I'm trying to marshall an instance of class Foo:

com.sun.istack.SAXException2: unable to marshal type "Foo" as 
an element because it is missing an @XmlRootElement annotation

What is a workaround?


Solution

  • Can you get away without the @XmlRootElement annotation on the superclass. Instead you should put it on each of your subclasses. JAX-B will still know about the superclass fields.