I have a complex hierarchy of Java interfaces that I'd like to marshal (and not necessarily unmarshal) with JAXB. These interfaces represent objects that will be returned from a JAX-RS REST API as XML, JSON, YAML, etc. (I'm using RestEasy, which can marshal JAXB-annotated types in formats other than XML.)
The problem seems to be that JAXB is fundamentally class-oriented. I've done lots of web research on the difficulties with JAXB and interfaces, the closest solutions being MOXy JAXB - Map Interfaces to XML and JAXB and Interface Fronted Models. However, I have two main issues: a) I want to annotate/work in terms of interfaces, not the concrete classes (of which there will be multiple implementations and containing significant other state that should not be marshaled), and b) I have multiple levels of interface inheritance. Here's a sample of the interfaces, minus any JAXB annotations so far:
interface Uuided {
UUID getId();
}
interface Named {
String getName();
}
interface Component extends Uuided, Named {
Map<String, ComponentAttribute> getAttributes();
}
interface Attribute extends Named {
Type getType();
Object getValue();
}
interface ComponentAttribute extends Attribute {
Component getDeclaringComponent();
}
Ideally, this would generate something like:
<component id="xxx" name="thing">
<attributes>
<componentAttribute name="color">
<type><stringType/></type>
<value>green</value>
<declaringComponent idref="xxx"/>
</componentAttribute>
</attributes>
</component>
Clearly, in the abstract, this leads to issues like determining the most derived annotated interface, of which there could theoretically be more than one. In my case, however, I believe the concrete classes only implement a single interface that should be marshaled. Unmarshaling is not necessary, as I have separate classes defining upsert properties.
So my question is, is this even possible to handle with JAXB, and if so, how? Even if I have to be very explicit in defining bindings, adapters, etc., I'd like to work within the JAXB framework to get the benefit of all the non-XML providers in RestEasy.