Monday 6 March 2017

Annotations of JAXB classes using Annox for Polymorphism


Introduction

I'm using a version 1.99 version of Jackson, which is a JSON binding framework that comes with JBOSS EAP server. I have come across this use cases that I have JSON Rest services for which I want to use PolyMorphism. By default, this isn't possible with JSON. You need to annotate your generated classes to get Polymorphism. For my project, we are using JAXB databindings, and luckily, Jackson can consume JAXB annotations as well.

Scope

I will demonstrate how I got Polymorphism to work with JAXB and JACKSON.

Schema


 <?xml version="1.0" encoding="UTF-8"?>  
 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"  
      targetNamespace="http://www.example.org/NewXMLSchema" xmlns:tns="http://www.example.org/NewXMLSchema"  
      xmlns:annox="http://annox.dev.java.net" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"  
      elementFormDefault="qualified" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"  
      jaxb:version="1.0" jaxb:extensionBindingPrefixes="xjc annox">  
      <xs:element name="Holder">  
           <xs:complexType>  
                <xs:sequence>  
                     <xs:element ref="tns:myParent" minOccurs="0" maxOccurs="unbounded">  
                     </xs:element>  
                </xs:sequence>  
           </xs:complexType>  
      </xs:element>  
      <xs:element name="myParent" type="tns:Parent">  
      </xs:element>  
      <xs:complexType name="Parent" abstract="true">  
           <xs:sequence>  
                <xs:element name="surname" type="xs:string"></xs:element>  
           </xs:sequence>  
      </xs:complexType>  
      <xs:element name="Child">  
           <xs:complexType>  
                <xs:complexContent>  
                     <xs:extension base="tns:Parent">  
                          <xs:sequence>  
                               <xs:element name="Firstname"></xs:element>  
                          </xs:sequence>  
                     </xs:extension>  
                </xs:complexContent>  
           </xs:complexType>  
      </xs:element>  
      <xs:element name="Child2">  
           <xs:complexType>  
                <xs:complexContent>  
                     <xs:extension base="tns:Parent">  
                          <xs:sequence>  
                               <xs:element name="secondChild"></xs:element>  
                          </xs:sequence>  
                     </xs:extension>  
                </xs:complexContent>  
           </xs:complexType>  
      </xs:element>  
 </xs:schema>