java - JAXBContext 上下文路径与 @XMLSeeAlso

标签 java jaxb

我对 JAXB 的这两个方面感到困惑。

据我了解,当将冒号分隔的包名称作为上下文路径传递到 newInstance 时JAXBContext 的方法,JAXB 会将这些包下的所有类添加到其作用域中,以便在 marshall/unmarshall/validate...等操作中使用。

@XmlSeeAlso 注释指示 JAXBContext 绑定(bind)类。我见过的所有示例都使用此注释来仅指定其子类(我实际上无法说出原因)。

以下是我的问题:

1- Can the @XmlSeeAlso be omitted if the classes referred by the annotation are within the packages passed to JAXBContext newInstance method?

2- If the above is true, then why do we need this annotation in the first place, where we can simply pass ALL the packages containing the classes we need to the newInstance function?

3- If the above is not true, can we only use packages containing top level classes passed to the newInstance, and use the annotation @XmlSeeAlso to reference all children of these classes?

最佳答案

1- Can the @XmlSeeAlso be omitted if the classes referred by the annotation are within the packages passed to JAXBContext newInstance method?

是的,@XmlSeeAlso 用于指示 JAXBContext 查找 newInstance() 未加载的类。要让 JAXBContext 了解所使用的子类,您可以使用 @XmlSeeAlso 指定,或者在 newInstance 方法中传递所有子类。

2- If the above is true, then why do we need this annotation in the first place, where we can simply pass ALL the packages containing the classes we need to the newInstance function?

@XmlSeeAlso 方法主要在您自己创建 JAXB 类时使用。当您采用 XSD->JAXB 方法时,首选使用 JAXBContext.newInstance() 并用 : 分隔所有包。

关于java - JAXBContext 上下文路径与 @XMLSeeAlso,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41766140/

相关文章:

java - JAXB 解码 - Java 字段的 XML 属性

java - MOXy 中的全局 Java 类型适配器

java - 使用 DTD 文件导致的 JAXB 解码 XML 时出错

Java HTTPConnection 在下载之前检索文件大小

java - 变量可能尚未初始化(边缘加权图)

java - 为并发操作关闭 JMX 连接

通过 Jettison 和 JAXB 将 XML 包装列表转换为 JSON 数组

java - 包含其他对象集合的对象的 JAXB 解码问题

java - SWT KeyUp 事件监听器错过按键

java - OnBootReceiver 可以工作,但找不到 AlarmReceiver