I've found a couple articles on saving interfaces with JAXB (http://blog.bdoughan.com/2011/05/jaxb-and-interface-fronted-models.html), but is there any way to save interfaces if you don't know what class is implementing it? or if that class varies. I mean, that seems to be the point of interfaces...
For instance, let's say class A
has a variable using interface I
, which is implemented in B
, C
and D
(like below) and I want to save an instance of class A
. Note that the fields that need to be saved ARE NOT declared in the interface:
public interface I {
public void getMagicNumber();
}
public class A { //<--- Root element
I interface;
public void setI(I i) {
this.interface = i;
}
}
public class B implements I {
private magicNumber = 0; // Needs to be saved by JAXB
public void setMagicNumber(int i) {
this.magicNumber = i;
}
@Override
public int getMagicNumber() {
return magicNumber
}
}
public class C implements I {
@Override
public int getMagicNumber() {
return [generate random number]
}
}
public class D implements I {
public int n1; //Needs to be saved by JAXB
public int n2; //Needs to be save by JAXB
@Override
public int getMagicNumber() {
return n1*n2;
}
}