25

I have REST (Jersey) webservice that makes use of some data objects that are marshalled/unmarshalled to/from XML. The data objects are in a separate project/jar that the webservice war depends on.

I'm using MOXy as my JAXB implementation since I'm deploying to Glassfish and that's already included. I know I need a jaxb.properties file to set the JAXB implementation to MOXy with this entry:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

The question is, should the jaxb.properties file be included in the data object jar or in the webservice war or both?

bdoughan
  • 147,609
  • 23
  • 300
  • 400
sdoca
  • 7,832
  • 23
  • 70
  • 127

2 Answers2

30

If you don't want or can not use the jaxb.properties (you have a lot of package, the model is in a external jar, you want only java and no configuration files...), you can directly specify the JaxbContextFactory :

Do not create the context using :

JAXBContext jaxbContext = JAXBContext.newInstance(new Class[]{Person.class, ObjectFactory.class}, properties);

But instead, specify the factory to use :

JAXBContext jaxbContext = JAXBContextFactory.createContext(new Class[]{Person.class, ObjectFactory.class}, properties);

where the factory is :

import org.eclipse.persistence.jaxb.JAXBContextFactory;

It is exactly the same, but it is specified explicitly in the java code instead of implicitly in a configuration file.

GaspardP
  • 880
  • 1
  • 12
  • 24
12

You package the jaxb.properties file with your model classes. GlassFish does not include the MOXy bundle yet, but you can add it easily. Check out my blog for more info:

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • 9
    What if my model classes are spread in more than one directory? Do I need to place one `jaxb.properties` files in each folder? – George Jul 23 '15 at 07:02
  • Yes: "JAXB Providers must generate a jaxb.properties file in each package containing schema derived classes." – bebbo Jul 03 '22 at 06:26