javaxmlmaven-jaxb2-pluginjsonix

How to include types outside generated package in jsonix?


I've excluded some xml types from jaxb2 generation, due the corresponding classes already exists in Java. Generation is perfectly working and classes are founded.

When I'm trying to include the types by this tutorial, jsonix searches classes only in the package with generated jaxb2 classes. So it looks generated.entity.Position instead of entity.Position class.

...

    <jaxb:bindings node="//xs:complexType[@name='PositionType']">
            <jaxb:class ref="entity.Position"/>
    </jaxb:bindings>
    <jaxb:bindings node="//xs:complexType[@name='MoveType']">
            <jaxb:class ref="entity.Move"/>
    </jaxb:bindings>

    <jsonix:mapping package="entity">
        <jsonix:includes>
            <jsonix:type name="Position"/>
        </jsonix:includes>
    </jsonix:mapping>
...

Solution

  • Author of Jsonix here.

    Includes/excludes configuration does not what you probably think it does. It just filters types or elements you want to include in your mapping. This allows you to create smaller mappings with only stuff you need.

    Still, mappings are generated per namespace/package. You can't put types from different packages to one mapping. You can, however, create a module which would contain several packages. Configuration is like:

        <jsonix:module name="CSW_2_0_2">
            <jsonix:mapping package="net.opengis.csw.v_2_0_2" name="CSW_2_0_2"/>
            <jsonix:mapping package="org.hisrc.dc.elements.v_1_1" name="DC_1_1"/>
            <jsonix:mapping package="org.hisrc.dc.terms" name="DCT"/>
        </jsonix:module>
    

    Next, you're using jaxb:class/@ref. This is not good for Jsonix because with this you say "I don't care about XML Schema model, just use this Java class". So the schema compiler (I mean the underlying JAXB schema compiler XJC) does not generate appropriate models. To make it short, Jsonix Schema Compiler will not compile your Java Classes into XML<->JS mappings. It only does this, well, for XML Schemas.