Polymorphism in JAX-RPC web services

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.

Asked by: Adrian425 | Posted: 21-01-2022

Answer 1

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:

class Child 
    private Section section;
    private Question question;

   // Constructor, etc...

class Question 
    private String name;
    // other fields, getters, and setters

class Section 
    private String label;
    private Child[] children;
    // getters and setters

class Quiz 
    private Child[] elements;
    // getter and setter

Which requires the client to check which member of child is populated, and is horribly ugly, I know.

Answered by: Freddie957 | Posted: 22-02-2022

Answer 2

Maybe someone is still looking for it, it can be done in axis 1.4:

  1. Add next line into your section of axis web service deployment file (wsdd):

    <parameter name="emitAllTypesInWSDL" value="true" />
  2. Modify your task in ant build file to include 'extraClasses':

    <axis-java2wsdl ... extraClasses="..."></axis-java2wsdl>

    In extraClasses mention all classes which will be passed, since axis aren't able to guess which childs you'll be passing/returning as parameters.

Done, now you can pass derivated classes in methods accepts parent classes. Etc:

// server side class A { ...}
class B extends A {...}
class C extends A {...}

// ws
class wsAxis { public void processPolymorphCall(A obj); }

// client side
wsAxis.processPolymorphCall(new C());

// this will work now, as much as returning derivated classes in place of base class.

Answered by: Chelsea870 | Posted: 22-02-2022

Similar questions

java - Using Polymorphism in Spring Boot services?

In my Spring Boot app, I am thinking of using an approach as the following interface and service implementations: PDFService: public interface PDFService { String createPdf(UUID uuid); } BrandPDFService: @Service @RequiredArgsConstructor public class BrandPDFService implements PDFService { private fin...

java - Polymorphism not working correctly

I have an interface called Dictionary which has a method insert(). This interface is implemented by class BSTree, but I also have a class AVLTree which is a child class of BSTree. AVLTree redefines the insert() so it suits it's needs. Now if I type the following code: Dictionary data=new AVLTree(); data.insert();

Polymorphism or Inheritance in JSON with Java and Ruby

For context, we are storing most of our data as JSON strings. This works very well with Hadoop on the backend and is easy to handle in Ruby on the front end. My data types fit the natural pattern for inheritance. For simplicity, lets say I have a class Pet and a process FeedPet that feeds a pet. I also have a process WalkDog that only applies to Dog, which is a kind of pet. My data is organized such that I never ne...

java - Hibernate polymorphism

This is a Hibnerate polymorphism question and a data model design question; they are intertwingled. I've used Hibernate in the past, and have enjoyed it, but sometimes I find it difficult to think about anything but trivial designs. Not a knock on Hibernate; just an observation that ORM in general can be challenging. I think this is a Hibernate 101 question, but I am not sure. What I am trying to achieve may not...

polymorphism - Java polymorphic methods

In Java i have abstract class named Operation and three its subclasses called OperationActivation, OperationPayment and OperationSendEmail. ADDED FROM COMMENT: Operation* objects are EJB Entity Beans so I can't have business logic inside them. No I want to create processor class like this: public class ProcessOperationService { public void processOperation(Operation operation) { out.pri...

Question about Java polymorphism and casting

I have a class C. Class E extends it. E e = new E(); C c = new C(); Why is e = (E) c; Upon further review: though numeric conversions have the same syntax as casting objects, some confusion arose. At any event, the above does not give a compilation, but rather a runtime error - so a class can be casted to subclass in some instances (otherwise the code woul...

java - Ambiguous Polymorphism?

In situations where two interfaces apply to an object, and there are two overloaded methods that differ only by distinguishing between those interfaces, which method gets called? In code. interface Foo {} interface Bar {} class Jaz implements Foo, Bar {} void DoSomething(Foo theObject) { System.out.println("Foo"); } void DoSomething(Bar theObject) { System.out.println("Bar"); } Jaz j = n...

java - trouble making polymorphism defeat those switch/case statements

Continuing on previous questions (here, and here), I implemented a basic command pattern, created my command classes and coded to an interface, so when any command is used, t...

polymorphism - Why can't you reduce the visibility of a method in a Java subclass?

Why does the compiler give an error message when you reduce the visibility of a method while overriding it in the subclass?

Java Polymorphism problem

I've learned all these programming terms in Swedish so please bear with me.. I'm having problems calling a method in a subclass which should override a method in the superclass. Here is the class structure with removed code: public interface Movable { public void move(double delta); } public abstract class Unit implements Movable, Viewable{ public void move(double delta){ ...

java - polymorphism and n-tier applications

I have this doubt for a long time... hope anyone can enlight me. Suppose I have 3 classes in my model. abstract class Document {} class Letter extends Document {} class Email extends Document {} and a service class with a method that returns a Document (either a Letter or Email). class MyService { public Document getDoc(){...} } So in my contro...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)