我目前正在为客户端 - 服务器通信设计通用消息交换格式.在消息中,有一个字段将被每个特定的Web服务覆盖.简化示例如下:
class Message {
public BasicInfo basicInfo;
}
Run Code Online (Sandbox Code Playgroud)
BasicInfo是基类.所有派生类都不会有public no-args构造函数.所以我XmlAdapter为他们写了.
@XmlJavaTypeAdapter(...)
class SpecificInfoA extends BasicInfo {
// Detail omitted
}
Run Code Online (Sandbox Code Playgroud)
然后我更新了类Message,希望jaxb能够在运行时确定类并使用相应的适配器来转换对象...
@XmlRootElement
@XmlSeeAlso({SpecificInfoA.class})
class Message {
@XmlAnyElement
public BasicInfo basicInfo;
}
Run Code Online (Sandbox Code Playgroud)
但是当我创建JAXBContext:
JAXBContext context = JAXBContext.newInstance(Message.class);
Run Code Online (Sandbox Code Playgroud)
抛出异常:
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) …Run Code Online (Sandbox Code Playgroud)