How do I solve EclipseLink's (MOXy) 'getti

2019-07-30 03:33发布

问题:

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.

回答1:

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:

  • https://github.com/bdoughan/blog20110322