1

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?

asked Oct 18, 2013 at 14:24

1 Answer 1

1

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.

answered Mar 6, 2014 at 19:55
1
  • Good point, however the next step I'd think of would be to user wrapper classes for consistency in every case, otherwise one would have to know each time whether a list might or might not contain elements of different types. Commented Mar 7, 2014 at 8:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.