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 versions:

AEM 6.3

  • No extra dependencies required

AEM 6.2

AEM 6.1

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.

For AEM 6.1 and AEM 6.2 projects, ensure the Sling Models 1.3.x API dependency is included ABOVE the AEM uber-jar dependency.

<!-- Put this above AEM 6.2 and lower Uber-Jar -->
<dependency>
    <groupId>org.apache.sling</groupId>
    <artifactId>org.apache.sling.models.api</artifactId>
    <version>1.3.0</version>
    <scope>provided</scope>
</dependency>

Ensure the Maven SCR Plug-in as 1.23.0+.

<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-scr-plugin</artifactId>
    <version>1.23.0</version>
</plugin>

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.

View the code


Supporting materials

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy