JAXB @XmlElements to have minOccurs = 1(JAXB @XmlElements minOccurs = 1)
问题描述
所以我想要一个用@XmlElements 注释的列表,如下所示
So I want to have a list to be annotated with @XmlElements like the following
@XmlElements(
{
@XmlElement(name = "Apple", type = Apple.class),
@XmlElement(name = "Orange", type = Orange.class),
@XmlElement(name = "Mango", type = Mango.class)
}
)
public List<Fruit> getEntries() {
return fruitList;
}
我想知道是否有办法强制列表包含至少 1 个元素,因为现在,xsd 看起来像
I am wondering whether there is a way to enforce the list to contain at least 1 element, because right now, the xsd looks like
<xs:complexType name="fruitList">
<xs:sequence>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Apple" type="tns:apple"/>
<xs:element name="Orange" type="tns:orange"/>
<xs:element name="Mango" type="tns:mango"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
推荐答案
假设 Apple、Orange 和 Mango 是 Fruit 的子类,您可能需要使用 @XmlElementRef 注释
对应于 XML 模式中的替换组,而不是 entries
属性@XmlElements
对应于选择的概念.
Assuming that Apple, Orange, and Mango are subclasses of Fruit you may want to annotate the entries
property with @XmlElementRef
which corresponds to substitution groups in XML schema, rather than @XmlElements
which corresponds to the concept of choice.
@XmlElementRef
public List<Fruit> getEntries() {
return fruitList;
}
这假设 Apple、Orange 和 Mango 类扩展了 Fruit 类,并使用 @XmlRootElement
This assumes that the Apple, Orange, and Mango classes extend the Fruit class, and are annotated with @XmlRootElement
@XmlRootElement
public class Apple extends Fruit {
...
}
更多信息
- http://bdoughan.blogspot.com/2010/11/jaxb-and-inheritance-using-substitution.html
- http://bdoughan.blogspot.com/2010/10/jaxb-and-xsd-choice-xmlelements.html
这篇关于JAXB @XmlElements minOccurs = 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JAXB @XmlElements minOccurs = 1
基础教程推荐
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 降序排序:Java Map 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01