Java Web 服务/JAXB - 抽象超类

发布于 2024-08-30 00:12:26 字数 395 浏览 5 评论 0原文

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

可以手动编组/解组&将参数作为字符串传递,但我想避免它。有什么想法如何去做吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋风の叶未落 2024-09-06 00:12:27

您在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>

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>
背叛残局 2024-09-06 00:12:27

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

行家配置:

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

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>
妞丶爷亲个 2024-09-06 00:12:27

我有一个类似的问题,上面的评论没有解决。从 JAXB 解组期间的实例化异常链接的博客文章(抽象基类,带有@XmlSeeAlso 具体子类) 对我理解我真正在做什么非常有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文