How do I prevent JAXB from binding superclass methods of the @XmlRootElement when marshalling? How do I prevent JAXB from binding superclass methods of the @XmlRootElement when marshalling? xml xml

How do I prevent JAXB from binding superclass methods of the @XmlRootElement when marshalling?


According to this StackOverflow post:How can I ignore a superclass?

It is not possible with JAX-B to ignore the superclass without modifying the superclass.Quoting the relevant portion of that post:

Update2: I found a thread on java.net for a similar problem. That thread resulted in an enhancement request, which was marked as a duplicate of another issue, which resulted in the @XmlTransient annotation. The comments on these bug reports lead me to believe this is impossible in the current spec.


Just add

@XmlAccessorType(XmlAccessType.NONE)

in front of EACH superclass declaration (and the class itself).

In your case:

@XmlAccessorType(XmlAccessType.NONE)class NamedObject{    [ ... ]}

Remember that this has to be done really for each superclass, it is oftenforgotten when dealing with huge class dependency trees.

Interfaces, of course, don't need any JAXB annotations.


I know this question is quite old, but there is a kind of solution which works if your superclass is in the same package as its child.

Create a package-info.java in your package and insert

@XmlAccessorType(XmlAccessType.NONE)package my.package.with.classes;

Obviously, it sets XmlAccessType.NONE upon all classes in the package. Myself, I use it in every package in our domain model. Therefore, I'm pretty safe. However, if your class is 'out of reach', f.e. it's in the JDK, use the solution from the accepted answer in [JAX-B] How can I ignore a superclass?.

I hope it's helpful for anynone who stumbles upon this question as I did.