2

有一个Bean1带有 type 子列表的简单类BeanChild1

@XmlRootElement(name="bean")
@XmlAccessorType(XmlAccessType.PROPERTY)
public static class Bean1
{
  public Bean1()
  {
    super();
  }

  private List<BeanChild1> childList = new ArrayList<>();

  @XmlElement(name="child")
  public List<BeanChild1> getChildList()
  {
    return childList;
  }

  public void setChildList(List<BeanChild1> pChildList)
  {
    childList = pChildList;
  }
}

public static class BeanChild1 { ... }

我正在尝试覆盖类,以更改列表的类型。新的子类 (ie BeanChild2) 扩展了前一个子类 (ie BeanChild1)。

public static class Bean2 extends Bean1
{
  public Bean2()
  {
    super();
  }

  @Override
  @XmlElement(name="child", type=BeanChild2.class)
  public List<BeanChild1> getChildList()
  {
    return super.getChildList();
  }
}

public static class BeanChild2 extends BeanChild1 { }

所以,这是我测试它的方法:

public static void main(String[] args)
{
  String xml = "<bean>" +
               "  <child></child>" +
               "  <child></child>" +
               "  <child></child>" +
               "</bean>";
  Reader reader = new StringReader(xml);

  Bean2 b2 =  JAXB.unmarshal(reader, Bean2.class);
  assert b2.getChildList().get(0) instanceof BeanChild2; // fails
}

测试表明该列表仍然包含 的子级BeanChild1

那么,我怎样才能强制它用实例填充childList字段呢?BeanChild2

如果没有简单的解决方案,请随意发布更多创造性的解决方案(例如使用XmlAdapters, Unmarshaller.Listener,也许在父类或子类上附加注释......)

4

1 回答 1

0

无法更改(例如覆盖)@XmlElement超类的注释。至少不使用注释。

  • 使用什么并不重要@XmlAccessorType(例如FIELD, PROPERTY, PUBLIC, NONE)。
  • 如果您将注释放在字段或吸气剂上,这没有任何区别。

但是,有一个合理的替代方案。JAXB 的 MOXy 实现提供了在 xml 文件中定义元数据/绑定的能力。事实上,每个 java 注释都有一个 XML 替代方案。但它会变得更好:您可以将 java 注释和这些 xml 元数据结合起来。很酷的是 MOXy 将合并两个声明,并且在发生冲突的情况下,XML 定义的元数据获得更高的优先级。

假设Bean1类如上注释。然后可以在 xml 文件中重新定义绑定。例如:

<xml-bindings xml-accessor-type="PROPERTY">
  <java-types>
    <java-type name="Bean1">
      <xml-element java-attribute="childList" name="child" 
                   type="BeanChild2" container-type="java.util.ArrayList" />
    </java-type>
  </java-types>
</xml-bindings>

在创建上下文对象期间需要这个新的绑定文件。

// use a map to reference the xml file
Map<String, Object> propertyMap = new HashMap<>();
propertyMap.put(JAXBContextProperties.OXM_METADATA_SOURCE, "bindings.xml");

// pass this properyMap during the creation of the JAXB context.
JAXBContext context = JAXBContext.newInstance(..., propertyMap);

MOXy 将合并 java 注释和 XML 绑定,如果发生冲突,将应用 XML 定义的设置。在这种情况下,较早的@XmlElement(name=child)注释被替换为等效于 .xml 的 xml 定义@XmlElement(name=child, type=BeanChild2.class)

您可以在此处阅读有关 XML 绑定的更多信息。

于 2016-07-27T21:04:19.427 回答