Thursday, 14 February 2019

What is Sling Model Exporters?

Sling Models

Sling Models are business objects that represents sling resources or sling requset objects in AEM. In other way, Sling Models let you map Java objects to Sling resources.

Sling Models Exporter

Sling Model Exporters helps to export the model as a different Java object (serialized into a different format such as JSON) by adding annotations to Sling Model. The model is programmatically exported by calling the ModelFactory method exportModel().

Jackson exporter
 
Jackson exporter which is used in Sling allows to convert Sling Model into a Java Map object. Jackson JSON exporter helps exporting Sling Models as JSON objects, which can be accessible from other third party web service applications, Java script application etc.

Sling Model Exporter is available from Sling Models version v1.3.0. 

Pictorial representation of Sling Model Vs Sling Model Exporter

Click on image to see it big


Can I use Sling Model Exporters with versions prior to AEM 6.3?

Yes AEM 6.3 onward no extra dependencies required to use Sling Model Exporters.

But versions prior to AEM 6.3 requires installation of below bundled packages from,
Sling > Downloads
  • Models API 1.3.0+
  • Models Implementation 1.3.0+
  • Models Jackson Exporter 1.0.0
  • AEM 6.2 Communities/Livefyre - FP2

How to add exporter framework to an existing sling model?

1) Add a resource type to a model,

Add below line in @Model code block of Sling Model.

resourceType = "[The type of the resourcse which we are requesting]";

2) Add a new annotation,

@Exporter ( name = "jackson", extensions = "json")

Associate a model class with a resource in SlingDocumentation here

After building the code, invoke the resource with selector model.json

This will bring back the response in JSON format and the response is picked from the getter methods in the sling model class.

Sling Exporter Documentation

Demo Video

 

Read More:

Sling Model Vs WCMUsePOJO

2 comments:

  1. Thank you for the article, can you please write an article where we can read all the items in the dialog box (items) and export as a JSON

    ReplyDelete
    Replies
    1. Sure, I will come up with the article. Thanks Chethan.

      Delete