1

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;
    }
}
sinθ
  • 11,093
  • 25
  • 85
  • 121
  • 1
    Have you tried it so far? Did you get an error? In any case, you will need to annotate the interface `I` with: `@XmlSeeAlso({B.class}) @XmlSeeAlso({C.class}) @XmlSeeAlso({D.class})` – My-Name-Is Dec 26 '13 at 15:03
  • possible duplicate of [JaxB marhsalling using interfaces](http://stackoverflow.com/questions/16153378/jaxb-marhsalling-using-interfaces) – bdoughan Jan 02 '14 at 22:30

0 Answers0