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?

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.

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.
    – dendini
    Commented Mar 7, 2014 at 8:47

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.