AEM Sling Model Exporter

Sling model exporter allows us to export resource as model in the form of Java objects in JSON format.

It will export all properties defined in Sling model class in the form of java object in the form of JSON as an example given below.

Below is the syntax to create Sling Model exporter:

@Model(adaptables = Resource.class,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL,
resourceType = { "practice/components/practice" })
@Exporter(name = "jackson", extensions = "json")
public class PracticeExporterModel {

Click on this link to read more about @Model annotation and how sling exporter works.

@Exporter annotation helps us to export java object with the help of extensions property as json allows specific extension to generate and call Sling model.

There is one more property as selector having model as default value and we can override or provide other value as selector with the help of selector property.

Example

  1. Create component having below authorable dialog properties as title, description and country.

2. Create Sling model PracticeExporterModel.java class annotated with @Model annotation which will allows us to export properties in the form of JSON.

Create bean class having private variables, setter and getter methods. Annotate all bean fields using @Inject annotation as shown below.

Main role is of resourceType property is to declare component’s resource type.

package com.javadoubts.core.models;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.*;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;

@Model(adaptables = Resource.class,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL,
resourceType = { "practice/components/practice" })
@Exporter(name = "jackson", extensions = "json")
public class PracticeExporterModel {

@Default(values="Test")
@Inject
@Named("title")
protected String heading;

@Inject
@Optional
protected String description;

@Inject
@Optional
protected String country;

@PostConstruct
protected void init() { }

public String getHeading() {
return heading;
}

public void setHeading(String heading) {
this.heading = heading;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}
}

3. Hit below URL in browser will return json for that specific component.

http://localhost:4502/content/practice/us/en/test/jcr:content/body/practice.model.tidy.json

Note: To generate JSON use mode.tidy.json at the end of the URL.

Without Custom Sling Exporter class

It will return all the node properties using OOTB sling model exporter.

Expose Page JSON using sling exporter

Hitting Complete page URL having .mode.tidy.json at the end will also call custom Sling Model exporter created for particular resource Type or component as shown below. It will return JSON using OOTB sling model if custom exporter are not present.

If we have defined Sling model exporter for all the resources and components will allow us to export JSON for complete page in required format.

This strategy allows us to share page URL and expose data in JSON format to third party systems as shown below.

http://localhost:4502/content/practice/us/en/test.model.tidy.json

Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.

0