Develop Sling Model Exporters
This technical walk through walks through setting up AEM for use with Sling Model Exporter, enhancing an existing Sling Model using the Exporter framework to rendition as JSON, and how to use Exporter options and Jackson annotations to further customize the output.
Sling Model Exporter was introduced in Sling Models v1.3.0. This new feature allows new annotations to be added to Sling Models that define how the Model an can be exported as a different Java object, or more commonly, serialized into a different format such as JSON.
Apache Sling provides a Jackson JSON exporter to cover the most common case of exporting Sling Models as JSON objects for consumption by programmatic web consumers such as other web services and JavaScript applications.
Configuring AEM for Sling Model Exporter
Sling Model Exporter is a feature of the Apache Sling project and not directly bound to the AEM product release cycle. Sling Model Exporter is compatible with AEM 6.3 and later.
The use-case for Sling Model Exporter
Sling Model Exporter is perfect for leveraging Sling Models that already contain business logic that support HTML renditions via HTL (or formerly JSP), and expose the same business representation as JSON for consumption by programmatic Web services or JavaScript applications.
Creating a Sling Model Exporter
Enabling Exporter support on a Sling Model is as easy as adding the @Exporter
annotation to the Java class.
Applying Sling Model Exporter options
Sling Model Exporter supports passing per-model Exporter options to the Exporter implementation to drive how the Sling Model is finally exported. These options generally apply “globally” to how the Sling Model is exported, versus per data point which can be done via inline annotations described below.
Jackson Exporter options include:
Applying Jackson annotations
Exporters implementations may also support annotations that can be applied inline on the Sling Model class, that can provide a finer level of control how the data is exported.