2

I am using EclipseLink external mapping file to marshal Java objects to XML and JSON. Since my model classes are defined in different projects where i don't have access to add/modify any file or classes.

So how can i avoid keeping jaxb.index and jaxb.properties file in the packages where my model classes resides?

bdoughan
  • 147,609
  • 23
  • 300
  • 400
Navin
  • 31
  • 2

1 Answers1

2

JAVA MODEL

Belos is the Java model I will use for this example:

Foo

package forum11615376;

public class Foo {

    private Bar bar;

    public Bar getBar() {
        return bar;
    }

    public void setBar(Bar bar) {
        this.bar = bar;
    }

}

Bar

package forum11615376;

public class Bar {

    private String value;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

}

External Mapping File (oxm.xml)

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum11615376">
    <java-types>
        <java-type name="Foo">
            <xml-root-element name="FOO"/>
            <java-attributes>
                <xml-element java-attribute="bar" name="BAR"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

DEMO

The demo code below demonstrates how to specify the external mapping file.

Eliminate jaxb.properties

To eliminate the jaxb.properties file (which is the standard mechanism for specifying the JAXB provider, see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html), we will use the native MOXy APIs to bootstrap the JAXBContext.

Eliminate jaxb.index

In this example the oxm.xml file plays the same role as jaxb.index. Since we need to pass something in to create the JAXBContext we will use an empty Class[].

package forum11615376;

import java.util.*;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import org.eclipse.persistence.jaxb.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(1);
        properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum11615376/oxm.xml");
        JAXBContext jc = JAXBContextFactory.createContext(new Class[] {}, properties);

        Bar bar = new Bar();
        bar.setValue("Hello World");
        Foo foo = new Foo();
        foo.setBar(bar);

        Marshaller marshaller =jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(foo, System.out);
    }

}

OUTPUT

Below is the output from running the demo code. As you can see the mapping metadata was applied.

<?xml version="1.0" encoding="UTF-8"?>
<FOO>
   <BAR>
      <value>Hello World</value>
   </BAR>
</FOO>
bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • Thanks for the reply Blaise. Can we provide eclipselink jaxbprovider using jaxb.properties present in the classpath, is it necessary to keep this file in the same package as model classes? I can see in your example jaxb.properties is present in foo package but Bar is using the default jaxbContext but in my case all the model classes are present in other package (where i don't have any control) and i don't want to create any wrapper class for those model classes and keep jaxb.properties in that wrapper class package. – Navin Jul 24 '12 at 19:07
  • @Navin - Using the default mechanism, you just need one `jaxb.properties` file. It can be in any of the packages you are creating a `JAXBContext` on. It does not need to be in all of them. – bdoughan Jul 24 '12 at 19:13