使用Moxy作为JAXB实现并使用多个POJO包设置jaxb.properties [英] Using Moxy as JAXB Implementation and setting jaxb.properties with more than one POJO package

查看:156
本文介绍了使用Moxy作为JAXB实现并使用多个POJO包设置jaxb.properties的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用EclipseLink Moxy作为我的JAXB实现。我正在尝试这个,因为JDK中包含的默认实现似乎有一个UTF-8编码的硬编码缩进级别。

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.

我的问题是它似乎我必须将jaxb.properties文件放入包含JAXB POJO的每个包中。我的JAXB POJO由xjc生成,特别是'jaxb2-maven-plugin'。 POJO生成许多包。是否有可能设置使用的实现而不在这些包中创建冗余的jaxb.properties文件?

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?

推荐答案


我正在尝试这个,因为
JDK中包含的默认实现似乎具有UTF-8
编码的硬编码缩进级别8。

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.

JAXB参考实现确实有一个扩展属性,允许您控制缩进:

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

  • https://jaxb.java.net/2.2.7/docs/ch05.html#indent

至于 jaxb.properties ,当使用单个 JAXBContext 处理多个包时,只有一个包需要包含 jaxb .properties file。

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.

有一些不同的方法可以让你更轻松地使用MOXy:

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

MOXy提供了一个包装XJC的脚本,它将添加 jaxb。属性文件在适当的位置。

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

<ECLIPSELINK_HOME>/bind/jaxb-compiler.sh



使MOXy成为您环境中的默认JAXB提供者



您还可以利用 META-INF / services 机制将MOXy指定为默认的JAXB提供程序:

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. 在目录 META-INF / services中创建一个包含名为 javax.xml.bind.JAXBContext 的文件的JAR

  2. javax.xml.bind.JAXBContext 文件的内容必须 org.eclipse.persistence.jaxb.JAXBContextFactory

  3. 将该jar添加到类路径中。

  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.



使用Native MOXy API



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);
    }

}

这篇关于使用Moxy作为JAXB实现并使用多个POJO包设置jaxb.properties的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆