@XmlElement(name = "tags") List<Tag> tags;
This basically means that for each item in the list, create an item named <tags> . So essentially all you have is a <subject> element wrapping multiple <tags> .
A pair of parameters to get another top-level item
You can create a βtop-levelβ class to represent this, for example Tags
public class Tags { protected List<Tag> tags; @XmlElement(name = "tag") public List<Tag> getTags() { if (tags == null) { tags = new ArrayList<>(); } return tags; } public void setTags(List<Tag> tags) { this.tags = tags; } }
Then enter the Tags instance as the Sample property
@XmlRootElement(name = "sample") public class Sample { private Tags tags; @XmlElement(name = "tags") public void setTags(Tags tags) { this.tags = tags; } public Tags getTags() { return tags; } }
OR
An even simpler solution is to use @XmlElementWrapper
Creates a wrapper element around an XML view. This is primarily intended to be used to create an XML wrapper element around collections.
Using the source code, you can simply add the annotation to the list
@XmlRootElement(name = "sample") public class Sample { private List<Tag> tags; @XmlElementWrapper(name = "tags") @XmlElement(name = "tag") public List<Tag> getTags() { if (tags == null) { tags = new ArrayList<>(); } return tags; } public void setTags(List<Tag> tags) { this.tags = tags; } }
source share