views:

1439

answers:

3

I have a class that is annotated as the @XmlRootElement with @XmlAccessorType(XmlAccessType.NONE). The problem that I am having is that the superclass's methods are being bound, when I do not want them to be bound, and cannot update the class. I am hoping there is an annotation that I can put on the root element class to prevent this from happening.

Example:

@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Person extends NamedObject {

    @XmlElement
    public String getId() { ... }

}

I would expect that only the methods annotated @XmlElement on Person would be bound and marshalled, but the superclass's methods are all being bound, as well. The resulting XML then has too much information.

How do I prevent the superclass's methods from being bound without having to annotate the superclass, itself?

+2  A: 

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 often forgotten when dealing with huge class dependency trees.

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

ivan_ivanovich_ivanoff
I wanted to avoid having to go into the superclasses, but I have also come to this conclusion. Thanks for the answer.
Matt Fisher
+1  A: 

Hello all.

I'm facing the exact same problem. My superclass does not handle any JAXB annotations (it doesn't have to) and I would like my subclass not to include superclass properties while marshalling.

Adding the XmlAccesorType on superclass cannot be the solution as I have no way to modify the superclass.

Is there any other solution?

Have you found anything regarding this?
Matt Fisher
+1  A: 

According to this StackOverflow post: http://stackoverflow.com/questions/1386192/jax-b-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.

benvolioT