Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB: @XmlTransient on third-party or external super class

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?

like image 393
Phil Avatar asked Mar 30 '10 08:03

Phil


3 Answers

OK, this was a pain in the you-know-what. Finally, after sifting through many a blog postings, here's what I did,

added a package-info.java class in the 'third-party class' package like this,

@javax.xml.bind.annotation.XmlAccessorType(javax.xml.bind.annotation.XmlAccessType.NONE) package third-party-package;

In my case, it was just one package so it was easy. Obviously, you will have to do this for for every separate package structure. I haven't tried doing it at a master package level.

like image 53
HakunaM Avatar answered Nov 15 '22 05:11

HakunaM


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

like image 36
lexicore Avatar answered Nov 15 '22 06:11

lexicore


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

  • http://wiki.eclipse.org/EclipseLink/Examples/MOXy/EclipseLink-OXM.XML

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>

like image 35
bdoughan Avatar answered Nov 15 '22 06:11

bdoughan