AEM code snippets : export page data as json format using sling model exporter

 

In my previous article’s you have seen how to export page data as a JSON format using servlet, now, I am going to explain exporting page content as JSON using sling models exporter,  using sling model exporter you can get below advantages

  • No need to write servlet to export data as JSON format
  • You can use same code which is written using sling models to render content and to export as JSON format

The below things are developed on AEM 6.3 so, we do not need to install any other dependencies

Sling model exporter is very useful when you want to publish your data to third party system’s or consuming within the site using ajax request if you have sling models and wants to serialize them,  by providing @Exporter annotations to the existing sling models you can convert your model as CaaS based URL

Here, I am serializing list collections component data, the text property I am returning as it is and also returning child nodes of the configured path which is a nodePath property

sm-expo

 

Step 1: create a model

Here, I have created a simple list collection model which contains three properties getText(), getLinks() and getMessage() 

  • The getText() returns whatever data is authored in the authoring dialog
  • The getLinks() read path from the authoring dialog and return all child nodes
  • The getMessage() returns static text

If you see the below code, the sling model exporter is not only serializing injected properties it is also serializing  custom properties which are getMessage()

The resourceType is an optional one, here I am assigning it to my custom listcollection component, which is resourceType = “examples/components/content/listcollection”

The sling models exporter uses Jackson framework which helps us serialize models as JSON format so, you need to provide exporter name as “jackson” and extensions should be “json”

The selector is optional one, if you don’t provide anything then it will take default selector which is “model”

Once, the build and deployment is done then test it using HTL and Rest based Url

Step 2: Test it using HTL

Check below screenshot using HTL, I am rendering the list collection model data

sling-model-listcollection

The output is something like below

list-col

Step 3: Test it with the REST-based URL

If you hit the URL then you should see something like below, here the “model” is selector

list-collection-sling-model-exporter

Step 4: Ignore properties

In some use cases where you don’t want to export all the properties as JSON format, there, you can use Jackson ignore annotation, check the below code and I would recommend you to refer this link to get more information on jackson annotations Jackson Annotations, if you use this then the property will not be serialized

But, for this, you need to load Jackson dependencies

Step 5: prior versions of AEM

If you are working on prior version’s of AEM 6.3 then you must load below sling model 1.3.0 dependencies and remaining things are same

Download Code