How to annotate a list using @XmlElement?

前端 未结 3 1429
忘掉有多难
忘掉有多难 2020-12-29 03:08

I have the following annotation using javax.xml.bind.annotation.XmlElement:

@XmlElement         
public List getKeywords() {
    r         


        
3条回答
  •  甜味超标
    2020-12-29 03:52

    Above answer by - Blaise Doughan is completely correct

    Another simple way is , even if you don't write the - @XmlElementWrapper

     private List keywords;
    
     @XmlElementWrapper
        @XmlElement(name="keyword")
        public List getKeywords() {
            return keywords;
        }
    

    You can use it this way - write the XmlAccessorType on Class level , then XML element name will be same as the class member name - keywords

    import java.util.List;
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Content {
    
        private List keywords;
    
        public Content() {}
    
    
        public List getKeywords() {
            return keywords;
        }
    
        public void setKeywords(List keywords) {
            this.keywords = keywords;
        }  
    
    }
    

提交回复
热议问题