10

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?

alex.zherdev
  • 23,914
  • 8
  • 62
  • 56
  • don't know much about JAXB, but there must be a reason, why no instance can be created. Has xxx.yyy.ZZZ an public no argument constructor? – Tim Büthe Apr 22 '10 at 07:55
  • `xxx.yyy.ZZZ` is the name of an abstract superclass of the actual object that has been marshalled. The bad thing is that when unmarshalling, jaxb tries to instantiate this superclass instead of the actual class of the object being passed. – alex.zherdev Apr 22 '10 at 08:26
  • Is xxx.yyy.ZZZ in the system/classpath that is trying to unmarshall the call? – fish Apr 22 '10 at 09:24
  • surely it is, otherwise I guess there would be a different exception thrown – alex.zherdev Apr 23 '10 at 07:15
  • Posting some of your code might help... this could be some issue with the annotation syntax. Otherwise, maybe try using an interface instead of an abstract super class? – Gabriel Jiva Apr 26 '10 at 17:05

3 Answers3

9

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>
Heri
  • 2,114
  • 14
  • 13
1

I was solving the same issue today. I found EclipseLink MOXy JAXB implementation as working, but there is no separate jar or maven module available (it is only as whole eclipselink.jar, which is huge) Finally I tried the latest JAXB version (2.2.2) and surprisingly it worked well.

maven config:

    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.2.2</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.2.2</version>
    </dependency>
1

I had a similar Problem, which the comments above didn't solve. The Blogsposts linked from InstantiationException during JAXB Unmarshalling (abstract base class, with @XmlSeeAlso concrete sub class) were very helpful for me to understand what I was really doing.

Community
  • 1
  • 1