JAXB @XmlElements to have minOccurs = 1

前端 未结 2 1104
独厮守ぢ
独厮守ぢ 2021-01-02 04:32

So I want to have a list to be annotated with @XmlElements like the following

@XmlElements(
        {
            @XmlElement(name = \"Apple\", type = Apple         


        
相关标签:
2条回答
  • 2021-01-02 04:52

    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)
        }
    )
    
    0 讨论(0)
  • 2021-01-02 05:11

    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

    • http://bdoughan.blogspot.com/2010/11/jaxb-and-inheritance-using-substitution.html
    • http://bdoughan.blogspot.com/2010/10/jaxb-and-xsd-choice-xmlelements.html
    0 讨论(0)
提交回复
热议问题