1

我正在尝试 @XmlIDREF参考方法。注释是:

 /**
   * getter for Attribute/List<Attribute> attributes
   * @return attributes
   */
  @XmlElementWrapper(name="attributes")
  @XmlElements({
    @XmlElement(name="Attribute", type=AttributeJaxbDao.class)
  })
  @XmlIDREF
  public List<Attribute> getAttributes() { 

这导致错误消息:

javax.xml.bind.JAXBException: 
Exception Description: Since the property or field [attributes] is set as XmlIDREF, the target type of each XmlElement declared within the XmlElements list must have an XmlID property.  Please ensure the target type of XmlElement [Attribute] contains an XmlID property.
 - with linked exception:
[Exception [EclipseLink-50035] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.JAXBException
Exception Description: Since the property or field [attributes] is set as XmlIDREF, the target type of each XmlElement declared within the XmlElements list must have an XmlID property.  Please ensure the target type of XmlElement [Attribute] contains an XmlID property.]
    at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:832)
    at org.eclipse.persistence.jaxb.JAXBContext.<init>(JAXBContext.java:143)

虽然AttributeJaxbDao有注释:

 /**
   * getter for xsd:string/String id
   * @return id
   */
  @XmlAttribute(name="id")  
  @XmlID
  public String getId() { 

@XmlIDREF“标准使用” /有两个不同之处@XmlID

  1. AttributeJaxbDao 是派生类——注解在超类中
  2. @XmlElement属性是一个接口 - 类型已在注释中专门命名为 AttributeJaxbDao.class

在这种情况下如何出现错误?有没有办法解决这个问题。我想避免被迫不使用接口和泛化。

4

1 回答 1

2

注意: 我是EclipseLink JAXB (MOXy)负责人,也是JAXB (JSR-222)专家组的成员。

不幸的是,我们阻止了这个用例正常工作的 2 个错误。

这些错误已在 EclipseLink 2.4.2 和 2.5.0 流中得到修复。这些流目前正在开发中,可以从以下链接下载每晚构建的版本(从 2013 年 2 月 22 日开始):


完整示例

下面是一个比您问题中给出的模型稍微复杂一点的模型,我将用它来证明现在一切正常。

该类Foo有两个类型的属性List<Attribute>Attribute一个是接口。两者都带有注释,@XmlElements其中包含@XmlElement指定具体类型的注释。该attributeRefs属性也用 注释@XmlIDREF

import java.util.*;
import javax.xml.bind.annotation.*;

@XmlRootElement
public class Foo {

    @XmlElementWrapper(name="Attributes")
    @XmlElements({
        @XmlElement(name="AttributeFoo", type=AttributeJaxbDao.class),
        @XmlElement(name="AttributeBar", type=AttributeJaxbDao2.class)
      })
    List<Attribute> attributes = new ArrayList<Attribute>();;


    @XmlElementWrapper(name="AttributeRefs")
    @XmlElements({
      @XmlElement(name="AttributeRefFoo", type=AttributeJaxbDao.class),
      @XmlElement(name="AttributeRefBar", type=AttributeJaxbDao2.class)
    })
    @XmlIDREF
    List<Attribute> attributeRefs = new ArrayList<Attribute>();

}

属性

Attribute只是一个非常简单的界面。

public interface Attribute {

}

属性JaxbDao

这是一个Attribute包含一个注释的字段的实现,注释@XmlID将利用该字段@XmlIDREF

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class AttributeJaxbDao implements Attribute {

    @XmlAttribute(name="id")  
    @XmlID
    String id;

    String name;

}

属性JaxbDao2

这也是一个Attribute具有用 注释的字段的实现@XmlID

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class AttributeJaxbDao2 implements Attribute {

    @XmlAttribute(name="id")  
    @XmlID
    String id;

    String name;

}

演示

下面是一些演示代码,您可以运行这些代码来证明一切正常:

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

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Foo.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum14921547/input.xml");
        Foo foo = (Foo) unmarshaller.unmarshal(xml);

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

}

输入.xml/输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<foo>
    <Attributes>
        <AttributeFoo id="a">
            <name>A</name>
        </AttributeFoo>
        <AttributeBar id="b">
            <name>B</name>
        </AttributeBar>
        <AttributeFoo id="c">
            <name>C</name>
        </AttributeFoo>
    </Attributes>
    <AttributeRefs>
        <AttributeRefFoo>a</AttributeRefFoo>
        <AttributeRefBar>b</AttributeRefBar>
        <AttributeRefFoo>c</AttributeRefFoo>
    </AttributeRefs>
</foo>
于 2013-02-18T12:17:48.553 回答