tags:

views:

400

answers:

3

Hi,

I need some help regarding the following issue with JAXB 2.1.

Sample: I've created a SpecialPerson class that extends a abstract class Person. Now I want to transform my object structure into a XML schema using JAXB. Thereby I don't want the Person XML type to appear in my XML schema to keep the schema simple. Instead I want the fields of the Person class to appear in the SpecialPerson XML type.

Normally I would add the annotation @XmlTransient on class level into the Person code.

The problem is that Person is a third-party class and I have no possibility to add @XmlTransient here. How can I tell JAXB that it should ignore the Person class without annotating the class. Is it possible to configure this externally somehow?

Have you had the same problem before? Any ideas what the best solution for this problem would be?

+2  A: 

You can provide mappings for third-party classes using Annox.

lexicore
(+1) nice tip; I'll take one of those, thanks
skaffman
BTW, I'm the author.
lexicore
Ah, *that* lexi :)
skaffman
Thanks lexicore, i'll have a look at Annox.
Phil
Let me know if you need assistance.
lexicore
A: 

You can annotate your SuperPerson class with @XmlTransient, that will instruct JaxB not to automatically marshal all properties. And then annotate each getter (or field) you want to serialize with the relevant annotation.

This approach is not very elegant, but it should work

LiorH
I guess you meant SpecialPerson and not SuperPerson, right? The problem is I can't annotate getter and fields as they are defined in the Person class (the super class) and not in the subclass SpecialPerson.
Phil
Yes, I meant SpecialPerson... anyways, you can override the getter\setters add annotations on the overriding methods and simply call super... as I said, not very elegant but it should work :-)
LiorH
A: 

The EclipseLink JAXB (MOXy) implementation offers a means of representing the metadata as XML that you could use:

You can specify some of the metadata using annotations, and the rest as XML. Below is what your document would look like:

<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm">

    <java-types>

        <java-type name="Person" xml-transient="true"/>

    </java-types>

</xml-bindings>

Blaise Doughan