MOXy / JAXB接口注释 [英] MOXy/JAXB interface annotation

查看:90
本文介绍了MOXy / JAXB接口注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型,它包含一个带有一个带注释属性的接口,以及一个不重新注释该属性实现的具体实现者。为什么不正确解组(使用MOXy 2.5.0)?我得到一个正确构造的对象,但该属性从未绑定到XML:

I have a model that consists of an interface with one annotated property, and a concrete implementor that does not re-annotate the implementation of that property. Why doesn't this unmarshal correctly (using MOXy 2.5.0)? I get a correctly constructed object, but the property is never bound to the XML:

<!-- XML -->
<InterfaceImpl id="5150" />



/**
 * Annotated interface
 */
@XmlRootElement(name="IInterface")
public interface IInterface
{
    @XmlAttribute(name="id")
    public void setId(int id);
}

/**
 * Concrete implementor
 */
@XmlRootElement(name="InterfaceImpl")
public class InterfaceImpl implements IInterface
{
    private int m_id = -1;

    @Override
    public void setId(int id)
    {
        m_id = id;
    }   
}

/**
 * Unmarshal code
 */
File f = new File("src\\resources\\Interface.xml");
JAXBContext context = JAXBContext.newInstance(MY_PATH);
Unmarshaller u = context.createUnmarshaller();
InterfaceImpl i = (InterfaceImpl)u.unmarshal(f);            

如果我将IInterface更改为抽象类,它可以正常工作 - 不应该抽象类和接口处理方式相同?这是预期的行为还是已知问题?

If I change IInterface to an abstract class, it works correctly - shouldn't abstract classes and interfaces be handled the same way? Is this expected behavior, or a known issue?

谢谢!

推荐答案

oxm.xml

您可以使用EclipseLink JAXB(MOXy)的外部绑定文件使MOXy认为 IInterface 是超级的class $ code> InterfaceImpl 而不是 Object

You could use EclipseLink JAXB (MOXy's) external binding file to make MOXy think that IInterface is the super class of InterfaceImpl instead of Object.

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum16878949">
    <java-types>
        <java-type name="InterfaceImpl" super-type="forum16878949.IInterface"/>
    </java-types>
</xml-bindings>

演示

下面是在创建 JAXBContext 时如何指定映射文档。出于演示的目的,我在 InterfaceImpl 中添加了 getId 方法,因此我可以显示解组工作。

Below is how you can specify the mapping document when creating the JAXBContext. For the purpose of this demo I added a getId method to InterfaceImpl so I could show the unmarshalling worked.

package forum16878949;

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

public class Demo {

    private static String MY_PATH = "forum16878949";

    public static void main(String[] args) throws Exception {
        /**
         * Unmarshal code
         */
        Map<String, Object> properties = new HashMap<String, Object>(1);
        properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum16878949/oxm.xml");
        File f = new File("src/forum16878949/Interface.xml");
        JAXBContext context = JAXBContext.newInstance(MY_PATH, IInterface.class.getClassLoader(), properties);
        Unmarshaller u = context.createUnmarshaller();
        InterfaceImpl i = (InterfaceImpl)u.unmarshal(f);
        System.out.println(i.getId());
    }

}

Interface.xml

<?xml version="1.0" encoding="UTF-8"?>
<InterfaceImpl id="123"/>

输出

123

更多信息

  • http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html
  • http://blog.bdoughan.com/search/label/jaxb.properties

这篇关于MOXy / JAXB接口注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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