a list of users

I use Java Jersey 1.x to sort an object with multiple members, one of which is a list. All member variables are correctly sorted and returned with the correct return type. However, it does not include objectList in the returned data.

Example:

 @XmlRootElement public class ClassWithList { private String front; private int total; private ArrayList<AnotherPOJOObject> objectList; ... getters/setters 

Getter:

 public List<AnotherPOJOObject> getObjectList() { return objectList; } 

I debugged it and verified that the objectList is indeed populated with data. AnotherPOJOObject also annotated as XmlRootElement

+4
source share
3 answers

Thanks to the suggestion of basiljames, I was able to get closer to the answer. The real problem was that the AnotherPOJOOject List was not so simple. Each object had an untyped Map of its own, and this threw Marshaller into a fit because he always wants to know the type of object.

I assume that you have chosen this answer to make sure that every collection you collect has a clearly defined type!

0
source

Take a look at http://docs.oracle.com/javase/6/docs/api/javax/xml/bind/annotation/XmlAccessorType.html . It details how JAXB will attempt to serialize POJOs. In particular, please note that by default it is used only for public users, which means that "Each pair of common getters / setters and each public field are automatically bound to XML, if only annotating XmlTransient." In this case, I assume that you do not have an open setter field for objectList, so JAXB will not serialize it. To get a list for serialization, you can:

  • Add public setter method for objectList
  • Declare objectList as public (probably not a good idea)
  • Add the @XmlElement @XmlElement to the recipient to explicitly specify JAXB to marshal the list in XML.
+1
source

I ran into the same problem and solved it after trial and error.

Try annotating @XmlElementWrapper(name = "orders") to getObjectList() , and also type private List<AnotherPOJOObject> objectList;

+1
source

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


All Articles