I have the following annotation using javax.xml.bind.annotation.XmlElement:
@XmlElement
public List getKeywords() {
r
You need to leverage @XmlElementWrapper and @XmlElement.
Content
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlRootElement
public class Content {
private List keywords;
public Content() {}
@XmlElementWrapper
@XmlElement(name="keyword")
public List getKeywords() {
return keywords;
}
public void setKeywords(List keywords) {
this.keywords = keywords;
}
}
Demo
import java.util.*;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Content.class);
List strings = new ArrayList(2);
strings.add("foo");
strings.add("bar");
Content content = new Content();
content.setKeywords(strings);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(content, System.out);
}
}
Output
foo
bar
Below are links to a couple articles from my blog that provide additional information: