https://github.com/nebnagrom/jaxbInheritance
In this contrived example we have a Super Class House which has a member variable which is a String and an @XmlRootElement annotation. There are three child Classes of House:
- BlueHouse which has an additional Integer
- RedHouse which has an additional String
- GreenHouse which has an additional Boolean
The issue occured once I started trying to return HousingEstate objects from my RESTful resource. When you invoked the resource you only got back member variables from the House class, i.e. something like this:
{"houses":[{"houseName":"first blue"},{"houseName":"2 blue"}]}
When what I wanted was more like:
{"houses":[{"houseName":"first blue", "blueAttribute":1},
{"houseName":"2 blue", "blueAttribute":2}]}
After a bit of digging I found that I needed to add another annotation on the House class - @XMLSeeAlso like this:@XmlSeeAlso({ GreenHouse.class })
After I added that I started getting the results I wanted.
I also tried this without generics or just using the Super Class but this did not work either. Interestingly I found that:
- Using no generics meant that I got House member variables only, even when I annotated BlueHouse with annotation and added BlueHouses to the collection
- Using the parent class as the Generic gave me Marshalling exceptions, probably to be expected though.
Stack trace of error you get with the House based version over the fold:
[javax.xml.bind.JAXBException: class bmorgan.jaxbInheritance.model.BlueHouse nor any of its super class is known to this context.]
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerI
mpl.java:317)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(Marshalle
rImpl.java:161)
at com.sun.jersey.json.impl.BaseJSONMarshaller.marshallToJSON(BaseJSONMa
rshaller.java:106)
at com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider.writ
eTo(JSONRootElementProvider.java:143)
at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.writeTo
(AbstractRootElementProvider.java:157)
... 25 more
No comments:
Post a Comment