6

The jaxb.properties needs to be in the same package as the domain classes you are creating the JAXBContext on.

I am using Moxy's xml driven configuration since I doesn't want to use annotations or XJC generated objects. I have an existing domain classes that are spread across multiple packages. Does this mean that i need to have the jaxb.properties present in all those packages or there is a better alternative (Maybe writing my own implementation of some interface that can read from a jvm arg or something)?

Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327

1 Answers1

9

Does this mean that i need to have the jaxb.properties present in all those packages?

If you are creating your JAXBContext on classes, then you need to have a jaxb.properties file in at least one of the packages of the domain classes passed in. In the example below you could have a jaxb.properties file in either package1 or package2.

JAXBContext jc = JAXBContext.newInstance(package1.Foo.class, package2.Bar.class);

If you are creating your JAXBContext on package names, then you need to have a jaxb.properties files in at least one of the packages. Note that packages are separated by a ':'.

JAXBContext jc = JAXBContext.newInstance("package1:package2");

or there is a better alternative

My preference is to use the standard JAXB APIs with a jaxb.properties file to specify MOXy as the JAXB provider. Some people prefer using the native MOXy APIs to do this:

JAXBContext jc = org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(new Class[] {Foo.class, Bar.class}, null);

For More Information

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • In the link provided, I see the below as ouput if I specify the jaxb.properties in only 1 package. "class com.sun.xml.bind.v2.runtime.JAXBContextImpl". As you can see, the package in which the jaxb.props is not there, it is reverting back to the suns implementation. Are we not going to have issues with this? – Aravind Yarram Aug 03 '12 at 14:10
  • As long as the `jaxb.properties` file is in one of the packages of the classes passed to create the `JAXBContext` you are ok. That is what I tried to demonstrate with that post. Are you seeing different results when running that example? – bdoughan Aug 03 '12 at 14:33
  • I haven't yet run that example but reading your post raised this doubt to me as you didn't bring a closure on that. It would be good if you can update your post to explicitly state that "This behavior is OK". – Aravind Yarram Aug 03 '12 at 14:36
  • @Pangea - I have updated the post in an attempt to make this more clear. Thank you for bringing it to my attention. – bdoughan Aug 03 '12 at 14:56
  • I cannot put a .properties file in my java folders. I think this is also against the WTP 2.0 default setup of classpath and folders. Am I missing something? The moxy configuration seems quite chaotic. – atripes Apr 08 '15 at 10:40
  • I am trying to use the alternative mentioned here. That is helping to use Moxy without the properties file. But getting this exception "Invalid XPath for XMLDirectMapping/XMLCompositeDirectCollectionMapping. XPath must either contain an @ symbol for attributes or end in /text() for text nodes. For example: "@name" or "name/text()". Any idea whats wrong here? – Aparna Jul 26 '16 at 10:44
  • @Aparna - Do you know what property it is complaining about, it's type, and what annotation is on it? – bdoughan Jul 26 '16 at 19:17
  • 1
    @Blaise : Got my error. I was missing the text(). That corrected it. Thanks! – Aparna Jul 28 '16 at 09:10