KAD
KAD

Reputation: 11122

JAXBContext context path vs @XMLSeeAlso

I have gotten confused by these two aspects of JAXB.

As far as I understand, when passing colon separated package names as context path to newInstance method of JAXBContext, JAXB will add all the classes under these packages to its scope to be used in operations like marshall/unmarshall/validate...etc.

The @XmlSeeAlso annotation instructs the JAXBContext to bind classes. All the examples I've seen use this annotation to specify only its child classes (I couldn't tell why actually).

Below are my questions:

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?

Upvotes: 1

Views: 1559

Answers (1)

ulab
ulab

Reputation: 1089

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

yes, @XmlSeeAlso is used to instruct JAXBContext to look for classes that is not otherwise loaded by newInstance(). To let JAXBContext to know about the subclasses used, either you can specify with @XmlSeeAlso or pass all subclasses in the 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?

@XmlSeeAlso approach is used mostly when you are creating JAXB classes on your own. When you have XSD->JAXB approach, then JAXBContext.newInstance() with all packages separated by : is preferred.

Upvotes: 2

Related Questions