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 request flow

The traditional use-case for Sling Models is to provide a business abstration 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 Request Flow

sling-model-request-flow
  1. HTTP GET Request is made for a resource in AEM.

    Example: HTTP GET /content/my-resource.html

  2. Based on the request resource’s sling:resourceType, the appropriate Script is resolved.

  3. The Script adapts the Request or Resource to the desired Sling Model.

  4. The Script uses the Sling Model object to generate the HTML rendition.

  5. 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 doesnt naturally lend itself to the definition of these formats. 

Sling Model Exporter 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 inspecs the Sling Model object, and generates JSON using any "getter" methods as JSON keys, and the getter return values as the JSON values.

The direct serialization of Sling Models allows them to service both normal Web requests with their HTML responses created using the traditional Sling Model request flow (see above), but also expose JSON renditions that can be consumed by web services or JavaScript applications.

sling-model-exporter-request-flow

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.

  1. 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

  2. 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.

  3. 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) 

  4. 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.

  5. The Sling Exporter Servlet returns the JSON rendition of the Sling Model in the HTTP Response.

Note:

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.

Note:

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.

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