2

I've got the following piece of code which I've put together mainly based on tutorials on EclipseLink's website:

    Partner p = new Partner();
    p.setId(1);
    p.setKey("a");
    p.setName("this is the name");

    Map<String, Source> metadataSourceMap = new HashMap<String, Source>();
    metadataSourceMap.put("com.company.pas.entity.partner", new StreamSource("/com/company/pas/entity/mapping/partner-pojo2xml.xml"));
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, metadataSourceMap);
    JAXBContext jaxbContext = JAXBContext.newInstance(new Class[] {Partner.class}, properties);

When I try to run this piece of code, all I get is an exception:

Exception in thread "main" javax.xml.bind.JAXBException: property "eclipselink.oxm.metadata-source" is not supported
    at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:115)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:248)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:235)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:445)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:637)

I haven't included the "mapping file" (partner-pojo2xml.xml) here, as the exception occurs regardless of whether the mapping file is added to the metaDataSourceMap or not.

The relevant part from pom.xml can be found below.

<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>eclipselink</artifactId>
    <version>2.5.1</version>
</dependency>   

What am I doing wrong? I've tried too many combinations, but I just can't get it to run.

sbrattla
  • 5,274
  • 3
  • 39
  • 63

1 Answers1

5

You need to be sure you have a jaxb.properties file with the following entry:

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

When you are using Maven this file needs to be in a directory structure matching the package name of the classes used to bootstrap the JAXBContext. This structure needs to go under the src/main/resources folder. Below is a link to an example I have in GitHub:

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • Does this mean that jaxb.properties need to be in the package where my mapping files (e.g. "partner-pojo2xml.xml") file is located, and not the package where my actual POJO files are? I have tried to put this file in the same package as my POJO files (where also the ObjectFactory class required by JAXB is located). – sbrattla Mar 25 '14 at 18:15
  • @sbrattla - You need to make sure the `jaxb.properties` is under `src/main/resources` and not `src/main/java`. Here is a link to get my GitHub example as a ZIP so you can see this working: https://github.com/bdoughan/blog20110322/archive/master.zip – bdoughan Mar 25 '14 at 18:19