padde
padde

Reputation: 661

Using Moxy as JAXB Implementation and setting jaxb.properties with more than one POJO package

I am currently trying to use EclipseLink Moxy as my JAXB implementation. I am trying this, because the default implementation included in the JDK seems to have a hard coded indentation level of eight with UTF-8 encoding.

My problem is that it seems that I have to put a jaxb.properties file into every package that contains a JAXB POJO. My JAXB POJOs are generated by xjc, specifically by 'jaxb2-maven-plugin'. The POJOs are generated into numerous packages. Is it somehow possible to set the used implementation without creating redundant jaxb.properties files in these packages?

Upvotes: 2

Views: 4676

Answers (1)

bdoughan
bdoughan

Reputation: 148977

I am trying this, because the default implementation included in the JDK seems to have a hard coded indentation level of eight with UTF-8 encoding.

The JAXB reference implementation does have an extension property that allows you to control indenting:


As far as jaxb.properties, when dealing with multiple packages with a single JAXBContext only one of the packages needs to include the jaxb.properties file.

There are a few different things that can be done to make your use of MOXy easier:

Use MOXy's XJC Wrapper

MOXy offers a script that wraps XJC that will add the jaxb.properties file in the appropriate spot.

<ECLIPSELINK_HOME>/bind/jaxb-compiler.sh

Make MOXy the Default JAXB Provider in Your Environment

You could also leverage the META-INF/services mechanism to specify MOXy as the default JAXB provider:

  1. Create a JAR that contains a file called javax.xml.bind.JAXBContext in the directory META-INF/services
  2. The contents of the javax.xml.bind.JAXBContext file must be org.eclipse.persistence.jaxb.JAXBContextFactory
  3. Add that jar to your classpath.

Use the Native MOXy API

import java.io.File;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContextFactory.createContext("com.example.pkg1:org.example.pkg2", null, null);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("input.xml");
        Object object = unmarshaller.unmarshal(xml);

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

}

Upvotes: 4

Related Questions