3

I'm trying to understand JAXB and looking to initialize multiple objects using JAXB. I found an excellent question asked and answered in StackOverFlow.

How to marshalling the muliptle object using jaxb

So I'm looking through the code, trying to understand each piece. The first and key part (I think) that I'm having trouble understanding is the following line:

JAXBContext jc = JAXBContext.newInstance(JAXB2_Lists.class, JAXB2_Book.class);

Looking through the JAXBContext documentation, I'm unable to determine which newInstance method is being used. Guessing which one is being used, I'm not understanding it really.

http://docs.oracle.com/javaee/5/api/javax/xml/bind/JAXBContext.html#newInstance(java.lang.Class...)

My guess is that it is using the following overload

public static JAXBContext newInstance(Class[] classesToBeBound,
                                      Map<String,?> properties)
                               throws JAXBException

Parameters:

classesToBeBound - list of java classes to be recognized by the new JAXBContext.

What does it mean by "to be recognized"? Recognized in what way?

Please educate me on this issue. Thank you.

Community
  • 1
  • 1
JamesA
  • 115
  • 2
  • 10
  • Recognized in the way that those objects are meant to be marshalled and unmarshalled by JAXB, so JAXB must be taught about which classes they are, so that it recognizes them as valid JAXB targets (and scans the relevant annotations they contain) . – Arnaud Jul 13 '16 at 13:42
  • Thanks Berger, but it is the Book class that is being marshaled in the coded example, correct? The code has the List class first, so is it marshalling both the List and Book classes? – JamesA Jul 13 '16 at 13:48
  • Yes, in the example from your link, a List is marshalled, which contains Book objects (so Books are also marshalled "in cascade"), so both have to be "recognized" by JAXB . – Arnaud Jul 13 '16 at 14:32
  • Why aren't you providing these answers as an answer, so I can give you thumbs up? =) – JamesA Jul 13 '16 at 14:35

1 Answers1

1

Recognized in the way that those objects are meant to be marshalled and unmarshalled by JAXB, so JAXB must be taught about which classes they are, so that it recognizes them as valid JAXB targets (and scans/processes the relevant annotations they contain) .

In the example from your link, a List is marshalled, which contains Book objects (so Books are also marshalled "in cascade"), so both have to be "recognized"/"known" by JAXB .

Furthermore, the method you are actually calling is :

public static JAXBContext newInstance(Class... classesToBeBound)

From the description :

Not only the new context will recognize all the classes specified, but it will also recognize any classes that are directly/indirectly referenced statically from the specified classes.

Because your List references Book, I guess you can omit the Bookclass from the parameters, and simply call

JAXBContext jc = JAXBContext.newInstance(JAXB2_Lists.class);
Arnaud
  • 17,229
  • 3
  • 31
  • 44
  • Did I identify the correct overload method? So for the Class[] classesToBeBound, one can provide a list of Class objects? So how does the "Map properties" parameter get involved? That isn't explained at all, as I'm able to determine from the documentation I provided. Thanks for your help. =) – JamesA Jul 13 '16 at 14:56
  • You are not calling this method, I updated the answer to bring more details. – Arnaud Jul 13 '16 at 15:07
  • I have one small doubt with regards to `JAXB new instance`. I need to `unmarshal` the classes which are present in the different `maven submodule`. I know now how to pass the `package` name when in the same `maven module or project` but if the classes are present in different `submodules` then how can I pass the `package` name? Do I need to pass the `package` name along with the `submodule` information? if so then can you provide me an example? I tried a. lot of things and searched many answers but could not find anything. – BATMAN_2008 May 24 '21 at 07:20