Spring MVC Jaxb2Marshaller does not handle inheritance class properly

BioPandemic

New Member
I have a Spring MVC controller to generate XML, it generates regular objects without any problem. However, looks like it does not support polymorphism properly. I guess it might be configuration issue.Here is my class hierarchy.\[code\]abstract class Base { String attr1;}class Child1 { String attrChild1;}class Child2 { String attrChild2;}@XmlRootElementclass MyList { @XmlElement (name="list") List<Base> lists;}\[/code\]Then I add 1 instance of Child1 and one instance of Child2 into lists.If I manually use JAXB to marshall it, it will generate some XML like this\[code\]<list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="child1"> <attr1>...</attr1> <attrChild1>...</attrChild1></list><list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="child2"> <attr1>...</attr1> <attrChild2>...</attrChild2></list>\[/code\]However, if I go through spring, I will only get\[code\]<list> <attr1>...</attr1></list><list> <attr1>...</attr1></list>\[/code\]Any suggestions? Here is my controller class\[code\]@RequestMapping(value="http://stackoverflow.com/rest/test", method=RequestMethod.GET, produces="application/xml")public @ResponseBody MyList getMyList() { MyList myList = ....; // add instance of Child1/2 return myList;}\[/code\]
 
Back
Top