I'm hoping to have a flexible way of marshalling objects. A verbose version for single objects and a less-verbose version for multiple object versions.
For example, consider my department model:
GET /locations/1:
<location id='1'>
<link rel="self" href="http://example.com/locations/1"/>
<link rel="parent" href="http://example.com/serviceareas/1"/>
<name>location 01</name>
<departments>
<department id='1'>
<link rel="self" href="http://example.com/departments/1"/>
<name>department 01</name>
</department>
<department id='2'>
<link rel="self" href="http://example.com/departments/2"/>
<name>department 02</name>
</department>
<department id='3'>
<link rel="self" href="http://example.com/departments/3"/>
<name>department 03</name>
</department>
</departments>
</location>
GET /department/1:
<department id='1'>
<link rel="self" href="http://example.com/departments/1"/>
<link rel="parent" href="http://example.com/locations/1"/>
<name>department 01</name>
<abbr>dept 01</abbr>
....
<specialty>critical care</specialty>
</department>
Is there a way to do this? Would I need to have separate entity objects? One that references the table for CRUD operations and another for lists?
Note: I'm the EclipseLink JAXB (MOXy) lead, and a member of the JAXB 2 (JSR-222) expert group.
Your question is tagged
EclipseLink
, if you are using EclipseLink JAXB (MOXy) you can take advantage of the external binding document to apply a second mapping to theDepartment
class.ContextResolver
In a JAX-RS environment you can leverage MOXy's external binding document through a
ContextResolver
:For More Information
External Binding Document
By default MOXy's external binding document is used to augment the annotated model, but if you set the
xml-mapping-metadata-complete
flag it will completely override the annotations allowing you to apply a completely different mapping:For More Information
UPDATE
This update is to address a number of questions you asked in one of your comments:
Yes each
ContextResolver
should have its own binding file. The main reason for introducing a newContextResolver
is to represent a secondary mapping.For a single
ContextResolver
you can express the metadata across multiple binding files, but they will be combined into a single set of mappings. This means that a singleContentResolver
cannot have multiple views of a single class. A separateContextResolver
is used to represent a secondary mapping.I recommend loading the metadata file from the class path.
Your JAX-RS implementation should pick up your
ContextResolver
because it is annotated with@Provider
. TheContextResolver
used for a class will depend on how you implement thegetContext
method:Here comes another idea. Could be a bad idea but somewhat easy.