1

I was investigating object marshaling and unMarshaling using JAXB. while noticed that there is two option of getting an instance of JAXBContext.

  • one is based on class: JAXBContext context = JAXBContext.newInstance(ex.getClass());
  • Other one is based on package name: JAXBContext context = JAXBContext.newInstance(ex.getClass().getPackage().getName());

For the second way, you have to provide jaxb.index file, containing list of bean class names.

Maybe someone can explain, what is the difference between this two methods of getting JAXBContext instance? Which is better to use and when?

Bublik
  • 912
  • 5
  • 15
  • 30
  • Possible duplicate of [JAXBContext.newInstance variations](http://stackoverflow.com/questions/16860759/jaxbcontext-newinstance-variations) – ulab Apr 12 '17 at 16:00

1 Answers1

2

For the second way, you have to provide jaxb.index file, containing list of bean class names.

This is not correct. In JAXB2 this works without jaxb.index as well, the classes are "recognized" via ObjectFactory and @XmlSeeAlso.

The usual approach is to use JAXBContext context = JAXBContext.newInstance("my.package:my.another.package); as you normally want to consider all of the relevant classes and don't want to enumerate them explicitly.

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • but still, what is the purpose of `jaxb.index` and when create it? – Bublik Apr 19 '17 at 13:57
  • @Bublik You can create it instead of the `ObjectFactory`. This might be more convenint if you write your JAXB classes by hand (vs. generating them from a schema). – lexicore Apr 19 '17 at 20:18