代码之家  ›  专栏  ›  技术社区  ›  alex.zherdev

Java Web Services/JAXB-抽象超类

  •  10
  • alex.zherdev  · 技术社区  · 15 年前

    我有一个包含jaxb注释类和抽象超类的包。我想在web服务接口中使用这个超类,所以 可以将任何子类作为参数传递。当我这样做时,会抛出一个异常:

    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]]
    

    可以手动将参数marshall/unmarshall&作为字符串传递,但我希望避免这样做。有什么办法吗?

    3 回复  |  直到 13 年前
        1
  •  9
  •   Heri    15 年前

    您是否在您的web服务请求中指定了具体的实现?这对我很有用:

    抽象基类:

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

    实现类:

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

    Web服务方法:

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

    请求:

    <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>
    
        2
  •  1
  •   Michal Moravcik    14 年前

    我今天也在解决同样的问题。 我发现eclipseelink moxy jaxb实现可以工作,但是没有单独的jar或maven模块可用(它只是整个eclipseelink.jar,非常大) 最后,我尝试了最新的jaxb版本(2.2.2),令人惊讶的是它工作得很好。

    Maven配置:

        <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>
    
        3
  •  1
  •   Community CDub    7 年前

    我有一个类似的问题,上面的评论没有解决。来自 InstantiationException during JAXB Unmarshalling (abstract base class, with @XmlSeeAlso concrete sub class) 很有助于我理解我真正在做什么。