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

后端 未结 5 1213
慢半拍i
慢半拍i 2021-01-06 03:10

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 transf

相关标签:
5条回答
  • 2021-01-06 03:34

    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>

    0 讨论(0)
  • 2021-01-06 03:34

    I posted another solution with complete code here

    JAXB External Custom Binding XJC Issue - Parsing results in empty node

    in case you are interested.

    0 讨论(0)
  • 2021-01-06 03:38

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

    0 讨论(0)
  • 2021-01-06 03:39

    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.

    0 讨论(0)
  • 2021-01-06 03:51

    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

    0 讨论(0)
提交回复
热议问题