在没有带有@XmlJavaTypeAdapter的no-args构造函数的类上是否可以使用@XmlSeeAlso?

时间:2011-11-28 06:58:22

标签: java jaxb

我目前正在为客户端 - 服务器通信设计通用的消息交换格式。在消息中,有一个字段将被每个特定的Web服务覆盖。简化示例如下:

class Message {
    public BasicInfo basicInfo;
}

BasicInfo是基类。所有派生类都不会有public no-args构造函数。所以我为他们写了XmlAdapter

@XmlJavaTypeAdapter(...)
class SpecificInfoA extends BasicInfo {
    // Detail omitted
}

然后我更新了类Message,希望jaxb能够在运行时确定类并使用相应的适配器来转换对象......

@XmlRootElement
@XmlSeeAlso({SpecificInfoA.class})
class Message {
    @XmlAnyElement
    public BasicInfo basicInfo;
}

但是当我创建JAXBContext

JAXBContext context = JAXBContext.newInstance(Message.class);

抛出异常:

com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
playground.SpecificInfoA does not have a no-arg default constructor.
    this problem is related to the following location:
        at playground.SpecificInfoA
        at @javax.xml.bind.annotation.XmlSeeAlso(value=[class playground.SpecificInfoA])

    at com.sun.xml.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:106)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:466)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:298)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:141)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1157)
    at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:145)

由于SpecificInfoA有一个XmlAdapter,我希望jaxb能够将它添加到它的上下文中,但是从上面的例外情况来看,它是不可能这样做的还是我做错了?

我知道我可以为XmlAdapter编写一个通用BasicInfo,在编组时将实例转换为通用绑定类型,并在解组时创建特定实例。但是它会将所有派生类紧密地耦合到一个适配器中,这是不希望的。

0 个答案:

没有答案