将JAXB与JgraphX一起使用?

时间:2013-09-01 20:40:56

标签: java jaxb jgraph jgraphx

我有一些使用JAXB注释映射的实体将它们转换为xml,但在这些实体中mxCell有一个对象,如何在不在代码库JgraphX中添加注释的情况下映射此对象?

有我的Objeto课程:

@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
@XmlSeeAlso({mxCell.class})
public abstract class ObjetoImpl implements Serializable, Objeto {

    @XmlAttribute
    protected String nome;

    @XmlAnyElement    
    protected mxCell cell;


    @Override
    public String toString() {
        return this.nome;
    }
}

它给了我以下例外:

 com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
com.mxgraph.model.mxICell is an interface, and JAXB can't handle interfaces.
    this problem is related to the following location:
        at com.mxgraph.model.mxICell
        at public com.mxgraph.model.mxICell com.mxgraph.model.mxCell.getParent()
        at com.mxgraph.model.mxCell
        at @javax.xml.bind.annotation.XmlSeeAlso(value=[class com.mxgraph.model.mxCell])
        at ardis.model.conceitual.atributo.Atributo
        at protected java.util.List ardis.model.conceitual.ObjetoWithAttributeImpl.attributes
        at ardis.model.conceitual.ObjetoWithAttributeImpl
        at ardis.model.conceitual.entidade.Entidade
        at @javax.xml.bind.annotation.XmlSeeAlso(value=[class ardis.model.conceitual.entidade.Entidade])
        at ardis.model.conceitual.ModeloConceitual

当接口的实现未正确映射到Jaxb时,会发生该异常,但我不想进入jgraphx库并对其进行修改

2 个答案:

答案 0 :(得分:2)

您可以通过以下几种方式处理此用例:


选项#1 - 使用@XmlElement

指定Impl类

对于接口类型的字段/属性,可以使用@XmlElement注释来指定具体实现。

@XmlElemen(type=mxCellImpl.class)    
protected mxCell cell;

了解更多信息


选项#2 - 使用XmlAdapter

XmlAdapter允许您在编组/解组过程中将不可映射的对象转换为可映射的对象。您可以使用它将JgraphX转换为您自己的域对象。

了解更多信息

答案 1 :(得分:1)

我无法使用JAXB转换具有mxCell内部的程序对象,因此我的解决方案是使用JgraphX“getXml”来转换图形元素和每个单元格的值。之后,我得到了单元格的值,并在我的代码中使用。

将图表传递给xml的代码:

        mxCodec codec = new mxCodec();


    String xml = mxXmlUtils.getXml(codec.encode(graph.getModel()));
相关问题