Returning Single Result, But Multiple Types From Web Service? In Java?

100 views Asked by At

I need to return a multiple values from a web service as...

Object GetResults(){}

Object can be several different types. How do I add these known types to the web service contract? Is there an annotation that I can decorate my interface with?

NOTE: I am using Java with CXF, Aegis, with Annotations.

Another variation of the same problem is returning an abstract class, but needing to send a concrete type.

Employee getEmployee(string name) {}

abstract class Employee {}

class SalariedEmployee extends Employee {}

In WCF and .NET there is a feature called KnownTypes that handles these scenarios.
Is there something similar in Java with CXF, Aegis, with Annotations?

1

There are 1 answers

0
Richard Crane On

There is an annotation in JAXB called @XmlSeeAlso. This can be used to decorate your Employee base class with the descendant types. Unfortunately Aegis databinding does not leverage this annotation. However, the default JAXB databinding in CXF does respect this annotation. This solves the SalariedEmployee scenario above. The equivalent to this is .NET is the KnownType attribute.

Unfortunately the first use case where we return an Object and need to ensure that all types that are valid to return from the web service are represented in the WSDL does not seem possible with code first development. In .NET you can use the ServiceKnownType attribute to expose these types. Unfortunately this just does not seem possible in Java with code first development.