If you are not using MOXy or just want to stick to standard JAXB annotations, you can extend upon Noby's answer to add support for a generic wrapper class. Noby's answer only currently supports a list of strings, but say for example you're going to be using the same generic wrapper class for several different classes. In my example, I want to create a generic "PagedList" class that will marshall to something that looks like a list, but also contains information about the page offset and the total number of elements in unpaged list.
The one downside of this solution is that you have to add additional @XmlElement mappings for each type of class that will be wrapped. Overall though, probably a better solution than creating a new class for each pagable elements.
@XmlType
public class PagedList<T> {
@XmlAttribute
public int offset;
@XmlAttribute
public long total;
@XmlElements({
@XmlElement(name="order", type=Order.class),
@XmlElement(name="address", type=Address.class)
// additional as needed
})
public List<T> items;
}
@XmlRootElement(name="customer-profile")
public class CustomerProfile {
@XmlElement
public PagedList<Order> orders;
@XmlElement
public PagedList<Address> addresses;
}
Marshalling this example would get you:
<customer-profile>
<order offset="1" total="100">
<order> ... </order>
<order> ... </order>
<order> ... </order>
...
</orders>
<addresses offset="1" total="5">
<address> ... </address>
<address> ... </address>
<address> ... </address>
<address> ... </address>
<address> ... </address>
<addresses>
</customer-profile>
Hope that helps. This is the solution that I settled upon at least.