简体   繁体   English

JAXB @XmlElements使minOccurs = 1

[英]JAXB @XmlElements to have minOccurs = 1

So I want to have a list to be annotated with @XmlElements like the following 所以我想要一个用@XmlElements注释的列表,如下所示

@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;
}

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 我想知道是否有办法强制列表包含至少1个元素,因为现在,xsd看起来像

<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>

I suggest to check: 我建议检查一下:

@XmlElements(
    {
        @XmlElement(name = "Apple", type = Apple.class, required = true),
        @XmlElement(name = "Orange", type = Orange.class, required = true),
        @XmlElement(name = "Mango", type = Mango.class, required = true)
    }
)

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. 假设Apple,Orange和Mango是Fruit的子类,您可能希望使用@XmlElementRef注释entries属性,该属性对应于XML模式中的替换组,而不是对应于选择概念的@XmlElements

@XmlElementRef
public List<Fruit> getEntries() {
        return fruitList;
}

This assumes that the Apple, Orange, and Mango classes extend the Fruit class, and are annotated with @XmlRootElement 这假定Apple,Orange和Mango类扩展了Fruit类,并使用@XmlRootElement进行了注释。

@XmlRootElement
public class Apple extends Fruit {
   ...
}

For More Information 欲获得更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM