I see that most implementations of JAX-RS represent a class object containing a list of elements as follows (assume a class House containing a list of People)
<houses>
<house>
<person>
<name>Adam</name>
</person>
<person>
<name>Blake</name>
</person>
</house>
<house>
</house>
</houses>
The result above is obtained for instance from Jersey 2 JAX-RS implementation,
notice Jersey creates a wrapper class “houses” around each house, however strangely it doesn’t create a wrapper class around each person!
I don’t feel this is a correct mapping of a list, in other words I’d feel more confortable with something like this:
<houses>
<house>
<persons>
<person>
<name>Adam</name>
</person>
<person>
<name>Blake</name>
</person>
</persons>
</house>
<house>
</house>
</houses>
Is there any document explaining how an object should be correctly mapped apart from any opninion?
XML documents can only have a single root node, so the <houses>
is necessary if you would want to serialize more than one <house>
.
A <persons>
element is not necessary as a node may contain any number of children with the same tag name – they can be disambiguated by position. It adds complication without adding value or structure. But such a grouping may be adequate e.g. when we have different kinds of inhabitants in our house:
<house>
<inhabitants>
<human>
<name>Larry</name>
</human>
<human>
<name>Guido</name>
</human>
<dog>
<name>Rasmus</name>
</dog>
</inhabitants>
</house>
Here, <inhabitants>
encodes a relation, whereas <human>
and <dog>
encode a type.
1