Reputation: 1022
I currently have this environment on my project:
public abstract class Foo {
private List<Thing> things;
public List<Thing> getThings() { return this.things; }
}
public abstract class Bar extends Foo {
@XmlElements({@XmlElement(name = "first", type = First.class)})
public List<Thing> getThings() { return super.getThings(); }
}
public class Bobar extends Bar {
@XmlElements({@XmlElement(name = "second", type = Second.class)})
public List<Thing> getThings() { return super.getThings(); }
}
For the following XML document
<bobar>
<first>blablabla</first>
<second>blublublu</second>
</bobar>
When I do
context = JAXBContext.newInstance("the.package.structure");
unmarshaller = context.createUnmarshaller();
Bar object = (Bar) unmarshaller.unmarshal("path-to-xml-document");
The Bar object
only has one element in the collection, not 2. The First
element is completly lost, when I try to do object.getThings()
, its size is 1 and the only object inside the collection is an instance of Second
. Can someone help me how can I achieve to get both objects in the collection? And if that's not possible, how can I achieve something similar to this?
The reason I'm doing this is that (in my project logic) every Bobar
s things collection has a First
in its collection, but not every Bar
has a Second
in its collection, and Foo
is a generic class.
When I change the order in my XML document, the output is different.
<bobar>
<second>blablabla</second>
<first>blublublu</first>
</bobar>
In this scenario, I get only an instance of First
in the collection, and Second
is lost. And changing the scenario more, I get interesting results:
public abstract class Foo {
private List<Thing> things;
public List<Thing> getThings() { return this.things; }
}
public abstract class Bar extends Foo {
@XmlElements({@XmlElement(name = "first", type = First.class), @XmlElement(name = "third, type = Third.class)})
public List<Thing> getThings() { return super.getThings(); }
}
public class Bobar extends Bar {
@XmlElements({@XmlElement(name = "second", type = Second.class)})
public List<Thing> getThings() { return super.getThings(); }
}
If I do
<bobar>
<third>bliblibli</third>
<second>blablabla</second>
<first>blublublu</first>
</bobar>
In theory, I think this shouldn't be validated against the XML Schema generated by that, as the order here is not correct. But besides that, in such scenario, I get Second
and First
, the Third
is lost.
Upvotes: 4
Views: 7666
Reputation: 148977
It is not possible to annotate a property on a super type, and have a sub try incrementally add to that mapping. Below is a way you could support all the use cases that you are after. One thing to be cautious of is that all levels in the object hierarchy would support the same set of elements. You would need to use a external means of validation to restrict the desired values.
If Thing
is a class not an interface, and First
and Second
extend Thing
then you may be interested in using @XmlElementRef
instead of @XmlElements
(see: http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-substitution.html). It will offer you more flexibility, at the cost of some validation (hard to restrict the set of valid values).
Bar
We will annotate the Bar
with @XmlTransient
so that the JAXB implementation doesn't process it.
package forum11698160;
import java.util.List;
import javax.xml.bind.annotation.XmlTransient;
@XmlTransient
public abstract class Bar extends Foo {
public List<Thing> getThings() {
return super.getThings();
}
}
Bobar
@XmlElementRef
corresponds to the concept of substitution groups in XML schema. The values matching the property will be based on @XmlRootElement
declarations.
package forum11698160;
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlRootElement
public class Bobar extends Bar {
@XmlElementRef
public List<Thing> getThings() {
return super.getThings();
}
}
Thing
As JAXB implementations can not use reflection to find all the subclasses of a type, we can use the @XmlSeeAlso
annotation to help out. If you don't use this annotation then you will need to include all the subtypes when bootstrapping the JAXBContext
.
package forum11698160;
import javax.xml.bind.annotation.XmlSeeAlso;
@XmlSeeAlso({First.class, Second.class})
public class Thing {
}
First
We will need to annotate First
with @XmlRootElement
:
package forum11698160;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class First extends Thing {
}
Second
Second
will also need to be annotated with @XmlRootElement
:
package forum11698160;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Second extends Thing {
}
Demo
package forum11698160;
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Bobar.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum11698160/input.xml");
Bobar bobar = (Bobar) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(bobar, System.out);
}
}
input.xml/Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bobar>
<first/>
<second/>
</bobar>
OTHER FILES
Below are the other files you need to run this example:
Foo
package forum11698160;
import java.util.*;
public abstract class Foo {
private List<Thing> things = new ArrayList<Thing>();
public List<Thing> getThings() {
return this.things;
}
}
Upvotes: 5
Reputation: 148977
Below is how you could map this use case by leveraging @XmlTransient
:
Bobar
You can't extend mappings in the subclasses, so you need to be sure when you do an @XmlElements
mapping you do everything (see: http://blog.bdoughan.com/2010/10/jaxb-and-xsd-choice-xmlelements.html).
package forum11698160;
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlRootElement
public class Bobar extends Bar {
@XmlElements({
@XmlElement(name = "first", type = First.class),
@XmlElement(name = "second", type = Second.class)
})
public List<Thing> getThings() {
return super.getThings();
}
}
Bar
You also can't override inherited properties. So we will need to use @XmlTransient
at the type level to indicate that we don't want our JAXB implementation to process the super type (see: http://blog.bdoughan.com/2011/06/ignoring-inheritance-with-xmltransient.html).
package forum11698160;
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlTransient
public abstract class Bar extends Foo {
public List<Thing> getThings() {
return super.getThings();
}
}
Demo
The following demo code can be used to demonstrate that everything works:
package forum11698160;
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Bobar.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum11698160/input.xml");
Bobar bobar = (Bobar) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(bobar, System.out);
}
}
input.xml/Output
<?xml version="1.0" encoding="UTF-8"?>
<bobar>
<first/>
<second/>
</bobar>
OTHER FILES
Below are the other files you need to run this example:
Foo
package forum11698160;
import java.util.*;
public abstract class Foo {
private List<Thing> things = new ArrayList<Thing>();
public List<Thing> getThings() {
return this.things;
}
}
Thing
package forum11698160;
public interface Thing {
}
First
package forum11698160;
public class First implements Thing {
}
Second
package forum11698160;
public class Second implements Thing {
}
Upvotes: 2