问题描述
我目前在我的项目中有这样的环境:
I currently have this environment on my project:
public abstract class Foo {
private List<Thing> things;
public List<Thing> getThings() { return this.things; }
}
public abstract class Bar extends Foo {
@XmlElements({@XmlElement(name = "first", type = First.class)})
public List<Thing> getThings() { return super.getThings(); }
}
public class Bobar extends Bar {
@XmlElements({@XmlElement(name = "second", type = Second.class)})
public List<Thing> getThings() { return super.getThings(); }
}
对于以下XML文档
<bobar>
<first>blablabla</first>
<second>blublublu</second>
</bobar>
当我这样做时
context = JAXBContext.newInstance("the.package.structure");
unmarshaller = context.createUnmarshaller();
Bar object = (Bar) unmarshaller.unmarshal("path-to-xml-document");
Bar 对象
只有一个元素集合,而不是2.当我尝试 object.getThings()
时,第一个
元素完全丢失了,它的大小为1,集合中唯一的对象是 Second
的实例。有人可以帮助我如何在集合中获取两个对象?如果那是不可能的,我怎样才能实现与此类似的东西?
The Bar object
only has one element in the collection, not 2. The First
element is completly lost, when I try to do object.getThings()
, its size is 1 and the only object inside the collection is an instance of Second
. Can someone help me how can I achieve to get both objects in the collection? And if that's not possible, how can I achieve something similar to this?
我这样做的原因是(在我的项目逻辑中)每个 Bobar
s集合在其集合中有一个 First
,但不是每个 Bar
在其集合中有一个第二
, Foo
是一个泛型类。
The reason I'm doing this is that (in my project logic) every Bobar
s things collection has a First
in its collection, but not every Bar
has a Second
in its collection, and Foo
is a generic class.
当我更改XML文档中的顺序时,输出不同。
When I change the order in my XML document, the output is different.
<bobar>
<second>blablabla</second>
<first>blublublu</first>
</bobar>
在这种情况下,我只得到一个 First $ c的实例集合中的$ c>和
第二
丢失。并且更改方案更多,我得到了有趣的结果:
In this scenario, I get only an instance of First
in the collection, and Second
is lost. And changing the scenario more, I get interesting results:
public abstract class Foo {
private List<Thing> things;
public List<Thing> getThings() { return this.things; }
}
public abstract class Bar extends Foo {
@XmlElements({@XmlElement(name = "first", type = First.class), @XmlElement(name = "third, type = Third.class)})
public List<Thing> getThings() { return super.getThings(); }
}
public class Bobar extends Bar {
@XmlElements({@XmlElement(name = "second", type = Second.class)})
public List<Thing> getThings() { return super.getThings(); }
}
如果我这样做
<bobar>
<third>bliblibli</third>
<second>blablabla</second>
<first>blublublu</first>
</bobar>
理论上,我认为这不应该针对由此生成的XML Schema进行验证,因为这里的顺序不正确。但除此之外,在这种情况下,我得到第二个
和第一个
,第三个
丢失了。
In theory, I think this shouldn't be validated against the XML Schema generated by that, as the order here is not correct. But besides that, in such scenario, I get Second
and First
, the Third
is lost.
推荐答案
无法在超类型上注释属性,并且有一个子尝试逐步添加到该属性映射。以下是一种可以支持您所使用的所有用例的方法。需要注意的一件事是,对象层次结构中的所有级别都支持同一组元素。您需要使用外部验证方法来限制所需的值。
It is not possible to annotate a property on a super type, and have a sub try incrementally add to that mapping. Below is a way you could support all the use cases that you are after. One thing to be cautious of is that all levels in the object hierarchy would support the same set of elements. You would need to use a external means of validation to restrict the desired values.
如果 Thing
是一个类而不是一个接口,第一个
和第二个
扩展事情
然后你可能有兴趣使用 @XmlElementRef
而不是 @XmlElements
(参见:)。它会以一些验证为代价提供更大的灵活性(很难限制有效值的设置)。
If Thing
is a class not an interface, and First
and Second
extend Thing
then you may be interested in using @XmlElementRef
instead of @XmlElements
(see: http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-substitution.html). It will offer you more flexibility, at the cost of some validation (hard to restrict the set of valid values).
Bar
我们将使用 @XmlTransient
注释 Bar
,以便JAXB实现不处理它。
We will annotate the Bar
with @XmlTransient
so that the JAXB implementation doesn't process it.
package forum11698160;
import java.util.List;
import javax.xml.bind.annotation.XmlTransient;
@XmlTransient
public abstract class Bar extends Foo {
public List<Thing> getThings() {
return super.getThings();
}
}
Bobar
@XmlElementRef
对应于XML模式中替换组的概念。与属性匹配的值将基于 @XmlRootElement
声明。
@XmlElementRef
corresponds to the concept of substitution groups in XML schema. The values matching the property will be based on @XmlRootElement
declarations.
package forum11698160;
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlRootElement
public class Bobar extends Bar {
@XmlElementRef
public List<Thing> getThings() {
return super.getThings();
}
}
东西
由于JAXB实现不能使用反射来查找类型的所有子类,我们可以使用 @XmlSeeAlso
注释帮助。如果你不使用这个注释,那么你需要在引导 JAXBContext
时包含所有子类型。
As JAXB implementations can not use reflection to find all the subclasses of a type, we can use the @XmlSeeAlso
annotation to help out. If you don't use this annotation then you will need to include all the subtypes when bootstrapping the JAXBContext
.
package forum11698160;
import javax.xml.bind.annotation.XmlSeeAlso;
@XmlSeeAlso({First.class, Second.class})
public class Thing {
}
首先
我们需要注释首先
, @XmlRootElement
:
package forum11698160;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class First extends Thing {
}
第二
第二
也需要用注释@XmlRootElement
:
package forum11698160;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Second extends Thing {
}
演示
package forum11698160;
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Bobar.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum11698160/input.xml");
Bobar bobar = (Bobar) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(bobar, System.out);
}
}
input.xml /输出
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bobar>
<first/>
<second/>
</bobar>
其他文件
以下是运行此示例所需的其他文件:
Below are the other files you need to run this example:
Foo
package forum11698160;
import java.util.*;
public abstract class Foo {
private List<Thing> things = new ArrayList<Thing>();
public List<Thing> getThings() {
return this.things;
}
}
这篇关于JAXB继承冲突 - 在子类上重新注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!