Last active
August 29, 2015 14:17
-
-
Save dmikurube/64582b923e41060d9e84 to your computer and use it in GitHub Desktop.
JAXB annotations in inheritance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.xml.bind.JAXB; | |
import javax.xml.bind.annotation.XmlElement; | |
import javax.xml.bind.annotation.XmlElementWrapper; | |
import javax.xml.bind.annotation.XmlRootElement; | |
import javax.xml.bind.annotation.adapters.XmlAdapter; | |
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; | |
@XmlRootElement(name = "base") | |
interface BaseClass { | |
@XmlElement(name = "foo") | |
public int getFooVar(); | |
@XmlElementWrapper(name = "bars") | |
@XmlElement(name = "bar") | |
public int[] getBarVar(); | |
} | |
class DerivedClass implements BaseClass { | |
@Override | |
public int getFooVar() { | |
return this.fooVar; | |
} | |
public void setFooVar(int fooVar) { | |
this.fooVar = fooVar; | |
} | |
@Override | |
public int[] getBarVar() { | |
return this.barVar; | |
} | |
public void setBarVar(int[] barVar) { | |
this.barVar = barVar; | |
} | |
private int fooVar; | |
private int[] barVar; | |
} | |
public class JaxbInheritance { | |
public static void main(String[] args) { | |
DerivedClass d = new DerivedClass(); | |
d.setFooVar(1); | |
int[] array = new int[2]; | |
array[0] = 12; | |
array[1] = 14; | |
d.setBarVar(array); | |
JAXB.marshal(d, System.out); | |
} | |
} | |
/* | |
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | |
<derivedClass> | |
<barVar>12</barVar> | |
<barVar>14</barVar> | |
<fooVar>1</fooVar> | |
</derivedClass> | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment