我对 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/