Matt
Matt

Reputation: 3244

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

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?

Upvotes: 10

Views: 11499

Answers (6)

Nzall
Nzall

Reputation: 3555

A solution I have found, but which might not work for you depending on what you want to do, is to override the getters you want to ignore and let them return null. The JAXB specs, and by extension the implementations, ignore fields that contain a null value. Note that if you still need to be able to access the superclass value itself using the subclass, you may need to add a secondary accessor method that is not a getter and adjust your code accordingly.

Upvotes: 0

Dave
Dave

Reputation: 21924

Replace your JAX-B implementation with MOXy and you can do anything you want. It has a ton of extensions that go above and beyond normal JAX-B, one of which will allow you to ignore inherited properties, etc. It also supports moving JAX-B annotations to an XML mapping file so you can keep multiple sets of mappings.

Upvotes: 0

Zdenek F
Zdenek F

Reputation: 1899

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.

Upvotes: 3

benvolioT
benvolioT

Reputation: 4556

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.

Upvotes: 6

Laurent
Laurent

Reputation:

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?

Upvotes: 1

ivan_ivanovich_ivanoff
ivan_ivanovich_ivanoff

Reputation: 19453

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.

Upvotes: 5

Related Questions