2

I have a service which should be able to deliver "user profiles" under different level of details or different verbosity modes.

These user profiles are synchronized in a database with JPA2 and all is working fine. Using MOXy (EclipseLink) I also serialize in XML the profiles, and all is working fine here too.

However, the verbosity modes (BRIEF, SUMMARY, FULL) are implemented by the presence or the absence of specific elements in the user profiles serialized in XML (the element QueryHistory for instance, should not be serialized when using the BRIEF verbosity mode).

In few words: I want to specify to a JAXBContext that some elements must not be serialized and I want to specify it at runtime.

If I well understood, MOXy Extensible Models - Refresh http://blog.bdoughan.com/2011/06/moxy-extensible-models-refresh-example.html is able to do that, but is someone here know a simpler way to implement this feature?

njames
  • 502
  • 1
  • 5
  • 17

1 Answers1

2

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.

To support the concept of different views on your data I would recommend having a different instance of JAXBContext to represent each of the mappings: BRIEF, SUMMARY, FULL. MOXy allows you to apply several mappings to a domain model by leveraging its external mapping document.

Strategies

  1. Annotations to map full model and use XML metadata to reduce mappings
  2. Annotations to map minimal model and use XML metadata to expand mappings

Example Code

Three instances of JAXBContext built on the same class (UserProfile) but with different mappings controlled by MOXy's external mapping document.

JAXBContext briefContext = JAXBContext.newInstance(UserProfile.class);

Map<String, Object> summaryProperties = new HashMap<String, Object>(1);
summaryProperties.put("eclipselink-oxm-xml", "summaryMapping.xml");
JAXBContext summaryContext = JAXBContext.newInstance(new Class[] {UserProfile.class}, summaryProperties);

List<String> fullMappingXMLs = new ArrayList(2);
fullMappingXMLs.add("summaryMapping.xml");
fullMappingXMLs.add("fullMapping.xml");
Map<String, Object> fullProperties = new HashMap<String, Object>(1);
fullProperties.put("eclipselink-oxm-xml", fullMappingXMLs);
JAXBContext fullContext = JAXBContext.newInstance(new Class[] {UserProfile.class}, fullProperties);

Below is an example that demonstrates how a JAXBContext can be bootstrapped from multiple mapping documents.

Extensible Models

The ability to extend the mapping metadata in MOXy is aimed at developers looking to make a change to the metadata without bringing down a running application. An example of this is a domain model backing an online survey that needs to be enhanced to support a new question.

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • Thank you for the answer. However I don't understand how to use a mapping file over my existing model. Actually the best could be to have the model (classic JPA entities), and then using a mapping file prune the model. But as I tested it the mapping file is not taken in account. Even with a JAXBHelper.getJAXBContext(jc).refeshMetadata(); – njames Apr 10 '12 at 12:23
  • @njames - I have updated my answer with a strategy you can use. I suggest using different instances of `JAXBContext` for the different levels of mapping instead of refreshing the metadata. – bdoughan Apr 10 '12 at 13:28
  • Thanks for your clear answer! however I already worked this way actually. My question is: As I have an existing JPA/JAXB model with annotated class, is there a way to use a mapping file in order to "override" the existing model? My concerns is that I cannot change to the model (nor remove annotations to use only mapping files) – njames Apr 10 '12 at 13:39
  • @njames - Yes the external mapping document has a feature to completely override the annotations. Here is an example: http://blog.bdoughan.com/2011/09/mapping-objects-to-multiple-xml-schemas.html – bdoughan Apr 10 '12 at 13:50