覆盖JAXB非modifyable领域的Java类绑定 [英] Override JAXB binding in non-modifyable domain Java classes

查看:133
本文介绍了覆盖JAXB非modifyable领域的Java类绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了一整天试图找出这个问题(包括在本网站广泛搜索),但我无法找到答案我的问题。我试图做到这一点:

I have spent the whole day trying to figure out this problem (including extensive searching on this site), but I can't find an answer to my problem. I am trying to achieve this:


  • XML,而且我无法控制的一些现有的Java对象之间的转换

  • 在生成的/源XML元素的名称来自Java类的属性
  • 的名称不同
  • 我限于JAXB-2.0

  • 我可以介绍一个包装类,它可以包含注释

让我告诉你的就是我想要达到一个例子。假设Java类我有超过看起来无法控制这样的:

Let me show you an example of what I'm trying to achieve. Let's assume that the Java class I have no control over looks like this:

public class TopNoControlClass {

    private BottomNoControlClass bottomNoControlObject;

    public TopNoControlClass(BottomNoControlClass bottomNoControlObject) {
        super();
        this.bottomNoControlObject = bottomNoControlObject;
    }

    public BottomNoControlClass getBottomNoControlObject() {
        return bottomNoControlObject;
    }
    public void setBottomNoControlObject(BottomNoControlClass bottomNoControlObject) {
        this.bottomNoControlObject = bottomNoControlObject;
    }
}

和引用的类:

public class BottomNoControlClass {

    private String foo;

    public BottomNoControlClass(String foo) {
        super();
        this.foo = foo;
    }

    public String getFoo() {
        return foo;
    }
    public void setFoo(String foo) {
        this.foo = foo;
    }
}

和想象我想这一点编组的:

And imagine I want to get this out of the marshalling:

<?xml version="1.0" encoding="UTF-8"?>
<Top>
    <Bottom>
        <bar>XXX</bar>
    </Bottom>
</Top>

&LT;顶&GT; 的将映射到的 TopNoControlClass 的和的&LT;底部&GT; 的底部将映射到的 BottomNoControlClass &LT;酒吧方式&gt; 的将映射到的 BottomNoControlClass

The <Top> would map to the TopNoControlClass and <Bottom> Bottom would map to the BottomNoControlClass and <bar> would map to the foo property of BottomNoControlClass.

为了做到上述情况,我会坦然面对创建外部XML绑定,将状态的映射,但我想不通的方式来使用外部绑定文件中运行。我见过的迄今只有所有的例子在生成时使用的外部XML绑定(即作为参数传递给XJC)。

In order to do the above, I would be comfortable with creating an external XML binding that would state the mappings, but I can't figure a way to use that external binding file in runtime. All the examples I've seen so far only used external XML bindings at generation time (i.e. as a parameter to xjc).

我也不会有引入一个包装类,将覆盖类名和类属性名的,将参照类(一个问题,即 TopNoControlClass 的和的 BottomNoControlClass )。这将是容易构造的的JAXBContext 的与类和JAXB让做休息。但我无法弄清楚,注释应该如何看等。

I also wouldn't have a problem with introducing a wrapper class that would override the class names and class property names for the classes it would refer (i.e. TopNoControlClass and BottomNoControlClass). It would be easy to construct the JAXBContext with that class and let JAXB do the rest. But I can't figure out how that annotation should look like.

任何帮助将大大AP preciated

Any help would be greatly appreciated

雅罗斯拉夫

推荐答案

注意:我是的 的EclipseLink JAXB(莫西) 领导和的 JAXB(JSR-222)专家小组。

在MOXY实施JAXB 2(JSR-222),我们正是这种用例提供​​了一个外部映射文档。

In the MOXy implementation of JAXB 2 (JSR-222) we offer an external mapping document for exactly this use case.

外部元数据(oxm.xml)

下面是在映射文件会是什么样子为您的使用情况。对于这个例子的目的,把你的模型类在一个名为包 forum13318677 。欲了解更多信息,请参阅:的http://blog.bdoughan.com/2010/12/extending-jaxb-re$p$psenting-annotations.html

Below is what the mapping document would look like for your use case. For the purpose of this example put your model classes in a package called forum13318677. For more information see: http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum13318677">
    <java-types>
        <java-type name="TopNoControlClass">
            <xml-root-element name="Top"/>
            <xml-type factory-class="forum13318677.Factory" factory-method="createTopNoControlClass"/>
            <java-attributes>
                <xml-element java-attribute="bottomNoControlObject" name="Bottom"/>
            </java-attributes>
        </java-type>
        <java-type name="BottomNoControlClass">
            <xml-type factory-class="forum13318677.Factory" factory-method="createBottomNoControlClass"/>
            <java-attributes>
                <xml-element java-attribute="foo" name="bar"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

工厂

由于您的模型类没有0参数构造函数我需要创建一个工厂类(参见:的 http://blog.bdoughan.com/2011/06/jaxb-and-factory-methods.html )。这个类是在 @XmlType 注释的XML重新presentation配置。

Since your model classes did not have 0-argument constructors I needed to create a factory class (see: http://blog.bdoughan.com/2011/06/jaxb-and-factory-methods.html). This class was configured in the XML representation of the @XmlType annotation.

package forum13318677;

public class Factory {

    public TopNoControlClass createTopNoControlClass() {
        return new TopNoControlClass(null);
    }

    public BottomNoControlClass createBottomNoControlClass() {
        return new BottomNoControlClass(null);
    }

}

jaxb.properties

要指定MOXY作为JAXB(JSR-222)提供商,你需要包括一个名为 jaxb.properties 在同一个包为您的域类具有以下项文件(参见: http://blog.bdoughan.com/ 2011/05 /指定-的EclipseLink-MOXY-AS-your.html )。

To specify MOXy as your JAXB (JSR-222) provider you need to include a file called jaxb.properties in the same package as your domain classes with the following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html).

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

演示

演示code以下演示了如何利用MOXY的外部映射文件。请注意,即使MOXY用作JAXB提供上有没有MOXY编译时依赖关系。

The demo code below demonstrates how to leverage MOXy's external mapping document. Note that even though MOXy is used as the JAXB provider there are no compile time dependencies on MOXy.

package forum13318677;

import java.io.File;
import java.util.*;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String,Object>();
        properties.put("eclipselink.oxm.metadata-source", "forum13318677/oxm.xml");
        JAXBContext jc = JAXBContext.newInstance(new Class[] {TopNoControlClass.class}, properties);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum13318677/input.xml");
        TopNoControlClass object = (TopNoControlClass) unmarshaller.unmarshal(xml);

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

}

这篇关于覆盖JAXB非modifyable领域的Java类绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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