Java Web Services/JAXB - Abstract superclass

alex.zherdev picture alex.zherdev · Apr 22, 2010 · Viewed 15.4k times · Source

I have a package with JAXB annotated classes with an abstract superclass. I want to use this superclass in web service interface, so I can pass any of subclasses as a parameter. When I do it, an exception is thrown:

javax.xml.ws.WebServiceException: javax.xml.bind.UnmarshalException
- with linked exception:
[javax.xml.bind.UnmarshalException: Unable to create an instance of xxx.yyy.ZZZ
- with linked exception:
[java.lang.InstantiationException]]

It is possible to manually marshall/unmarshall & pass parameter as a string, but I would like to avoid it. Any ideas how to do it?

Answer

Heri picture Heri · Apr 24, 2010

Have you specified the concrete implementation in your web service request? This works fine for me:

Abstract base class:

@XmlSeeAlso({Foo.class, Bar.class})
public abstract class FooBase
{
  ...
}

Implementation class:

@XmlRootElement(name = "foo")
public class Foo extends FooBase
{
  ...
}

Web service method:

public String getFoo(@WebParam(name = "param") final FooBase foo)
{
  ...
}

Request:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.example/">
   <soapenv:Header/>
   <soapenv:Body>
      <ser:getFoo>
         <param xsi:type="ser:foo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
      </ser:getFoo>
   </soapenv:Body>
</soapenv:Envelope>