Understand Sling Model Exporter
Apache Sling Models 1.3.0 introduces Sling Model Exporter, an elegant way to export or serialize Sling Model objects into custom abstractions. This article juxtaposes the traditional use-case of using Sling Models to populate HTL scripts, with leveraging the Sling Model Exporter framework to serialize a Sling Model into JSON.
Traditional Sling Model HTTP Request flow
The traditional use-case for Sling Models is to provide a business abstraction for a resource or request, which provides HTL scripts (or, previously JSPs) an interface for accessing business functions.
Common patterns are developing Sling Models that represent AEM Components or Pages, and using the Sling Model objects to feed the HTL scripts with data, with an end result of HTML that's displayed in the browser.
Sling Model HTTP Request flow
- HTTP GET Request is made for a resource in AEM.Example: HTTP GET /content/my-resource.html
- Based on the request resource’s sling:resourceType , the appropriate Script is resolved.
- The Script adapts the Request or Resource to the desired Sling Model.
- The Script uses the Sling Model object to generate the HTML rendition.
- The HTML generated by the Script is returned in the HTTP Response.
This traditional pattern works well in the context of generating HTML as the Sling Model can be easily leveraged via HTL. Creating more structured data such a JSON or XML is a far more tedious endeavour as HTL doesn't naturally lend itself to the definition of these formats.
Sling Model Exporter HTTP Request flow
Apache Sling Model Exporter comes with a Sling provided Jackson Exporter that automatically serializes an "ordinary" Sling Model object into JSON. The Jackson Exporter, while quite configurable, at its core inspects the Sling Model object, and generates JSON using any "getter" methods as JSON keys, and the getter return values as the JSON values.
This flow describes the flow using the provided Jackson Exporter to produce JSON output. Use of custom exporters follow the same flow but with their output format.
- HTTP GET Request is made for a resource in AEM with the selector and extension registered with the Sling Model’s Exporter.Example: HTTP GET /content/my-resource.model.json
- Sling resolves the the requested resource’s sling:resourceType , selector and extension to a dynamically generated Sling Exporter Servlet, which is mapped to the Sling Model with Exporter.
- The resolved Sling Exporter Servlet invokes the Sling Model Exporter against the Sling Model object adapted from the request or resource (as determined by the Sling Models adaptables).
- The exporter serializes the Sling Model based on the Exporter Options and Exporter-specific Sling Model annotations and returns the result to the Sling Exporter Servlet.
- The Sling Exporter Servlet returns the JSON rendition of the Sling Model in the HTTP Response.
While the Apache Sling project provides the Jackson Exporter that serializes Sling Models to JSON, the Exporter framework also supports custom Exporters. For example, a project could implement a custom Exporter that serializes a Sling Model into XML.
Not only does Sling Model Exporter serialize Sling Models, it can also export them as Java objects. The exporting to other Java objects does not play a role in the HTTP Request flow, and thus does not appear in the above diagram.