Java Web 服务/JAXB - 抽象超类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2688878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:21:42  来源:igfitidea点击:

Java Web Services/JAXB - Abstract superclass

javaweb-servicesjaxbsuperclass

提问by alex.zherdev

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:

我有一个带有 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]]

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?

可以手动编组/解组并将参数作为字符串传递,但我想避免它。任何想法如何做到?

采纳答案by Heri

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

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

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>

回答by Michal Moravcik

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.

我今天正在解决同样的问题。我发现 EclipseLink MOXy JAXB 实现可以工作,但是没有单独的 jar 或 maven 模块可用(它只是整个 eclipselink.jar,这是巨大的)最后我尝试了最新的 JAXB 版本(2.2.2),令人惊讶的是它运行良好.

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>

回答by J?rg Pfründer

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.

我有一个类似的问题,上面的评论没有解决。JAXB Unmarshalling 期间InstantiationException链接的博客帖子(抽象基类,带有 @XmlSeeAlso 具体子类)对我了解我真正在做什么非常有帮助。