I have a JAX-RPC (Java) web service that needs to return a complex polymorphic value. To be more specific, the class structure is something like this:
abstract class Child { } class Question extends Child { private String name; // other fields, getters, and setters } class Section extends Child { private String label; private Child[] children; // getters and setters } class Quiz { private Child[] elements; // getter and setter }
My web service has a method that returns a Quiz, which of course may contain Questions and Sections which may contain Questions and other Sections, and so on and so forth. However, when I generate the WSDL, only Child and Quiz make it in. When I call the web service, I get back a Quiz element with the right number of children, but they’re all Child elements, and they’re all empty.
Is there a good way to make this work, short of just returning XML as a String?
Before anyone asks, due to circumstances beyond my control, I cannot use JAX-WS.
I don’t think JAX-RPC supports polymorphism in that way. I had a similar problem, and had to work around it by creating a class that had just two members – one for each of the two classes that could possibly be returned – and only populating one depending on the type I wanted to return. So in your case:
Which requires the client to check which member of child is populated, and is horribly ugly, I know.