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;
}
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>
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.
@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
@XmlRootElement
public class Apple extends Fruit {
   ...
}
For More Information
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With