Regarding REST and XMLElement answer

I have a REST answer that needs to be created in code:

<sample> <tags> <tag> <name>ABC</name> <Date>2014-10-14T12:30:05Z</ingress> </tag> <tag> <name>DEF</name> <Date>2014-10-14T12:30:05Z</ingress> </tag> </tags> </sample> 

However i get

 <sample> <tags> <name>ABC</name> <Date>2014-10-14T12:30:05Z</ingress> </tags> <tags> <name>DEF</name> <Date>2014-10-14T12:30:05Z</ingress> </tags> </sample> 

in the answer. Can someone please help me how to declare a Java class to get the desired REST answer?

Here is the java code:

 @XmlRootElement(name = "sample") public class Sample { private List<Tag> tags; @XmlElement(name = "tags") public List<Tag> getTags() { return tags; } /** * @param tags * the tags to set */ public void setTags(List<Tag> tags) { this.tags = tags; } } @XmlRootElement(name = "tag") public class Tag { private String name; private Date date; /** * @return the name */ @XmlElement(name = "name") public String getName() { return name; } /** * @param name * the name to set */ public void setName(String name) { this.name = name; } /** * @return the date */ @XmlElement(name = "date") public Date getDate() { return date; } /** * @param date * the date to set */ public void setDate(Date date) { this.date = date; } } 

thanks

+5
source share
2 answers
 @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; } } 
+3
source

You can simply use the @XmlElementWrapper annotation to add a grouping element to your collection.

 @XmlElementWrapper @XmlElement(name = "tag") public List<Tag> getTags() { return tags; } 

Note. @XmlElement applies to each item in the collection.

+1
source

Source: https://habr.com/ru/post/1204673/


All Articles