March 29, 2020
Estimated Post Reading Time ~

Overview of Sling Model Exporter in AEM 6.3

Sling Model Exporter was introduced in Sling Models v1.3.0. This new feature allows new annotations to be added to Sling Models that define how the Model can be exported as JSON.

Use Case: Now the question comes why sling model exporter. So the idea is if there is a sling model and you want to fetch the same properties as a JSON response, so there is no need to create a Sling Servlet. You just need to export your sling model using Jackson exporter and that’s all. Sling Model Exporter can be used as a web service or as a rest API.

In AEM 6.3, No external dependencies required.

Fig - Required Bundle for Sling Model Exporter

The felix console already contains:
Apache Sling Model API (1.3.0+)
Apache Sling Model Implementations (1.3.0+)
Apache Sling Model Exporter(1.0.6)

Now create a Sling Model, But before creating the sling model you need to add some dependency for building the project
<dependency>
    <groupId>org.apache.sling</groupId>
    <artifactId>org.apache.sling.models.api</artifactId>    <version>1.3.0</version>    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.apache.geronimo.specs</groupId>    <artifactId>geronimo-atinject_1.0_spec</artifactId>    <version>1.0</version>    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>    <artifactId>jackson-annotations</artifactId>    <version>2.8.4</version>    <scope>provided</scope>
</dependency>


Don’t forget to make an entry of sling model package in the maven-bundle plugin.


Fig - Sling Model Package entry in bundle POM.xml

Now Let’s take an example of a” Hero Image” component of we-retail:

Fig- Hero Component

The properties of the above component are being stored here:


Fig - Dialog Values of Hero Component

Sling Model without Exporter:

package com.mycompany.myproject.sling.models;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;

import javax.inject.Inject;
import javax.inject.Named;

@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class ModelComponent {

@Inject
String title;

@Inject
String heading;

@Inject
String buttonLabel;

@Inject
String buttonLinkTo;

@Inject
String fileReference;

@Inject
@Named("sling:resourceType")
String slingResourceType;

public String getHeading() {
return heading;
}

public String getButtonLabel() {
return buttonLabel;
}

public String getButtonLinkTo() {
return buttonLinkTo;
}

public String getSlingResourceType() {
return slingResourceType;
}

public String getTitle() {
return title;
}

public String getFileReference() {
return fileReference;
}

}


To enable the sling model as an exporter, the @Exporter annotation is being used.

package com.mycompany.myproject.sling.models;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Exporter;
import org.apache.sling.models.annotations.ExporterOption;
import org.apache.sling.models.annotations.Model;

import javax.inject.Inject;
import javax.inject.Named;

@Model(adaptables = Resource.class, resourceType = { "weretail/components/content/heroimage" }, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
@Exporter(name = "jackson", extensions = "json", options = { @ExporterOption(name = "SerializationFeature.WRITE_DATES_AS_TIMESTAMPS", value = "true") })
public class ModelComponent {

@Inject
String title;

@Inject
String heading;

@Inject
String buttonLabel;

@Inject
String buttonLinkTo;

@Inject
String fileReference;

@Inject
@Named("sling:resourceType")
String slingResourceType;

public String getHeading() {
return heading;
}

public String getButtonLabel() {
return buttonLabel;
}

public String getButtonLinkTo() {
return buttonLinkTo;
}

public String getSlingResourceType() {
return slingResourceType;
}

public String getTitle() {
return title;
}

public String getFileReference() {
return fileReference;
}
}


Make a rest call with the following format:
${crx.host}:${crx.port}${path-to-resource}.${selector}.tidy.${extension}

Note:”tidy” is a default selector to show the json response in a clean format.It is not mandatory at all.

Fig - Hero Component Dialog value in JSON Response from Sling Model Exporter

@Model annotation contains:

1. resourceType: This property is of String or String[] type.
The resourceType helps to know that for which resources ,the sling model needs to be exported.

@Exporter annotation contains:

1.name: Sling model provides "jackson" as exporter.
2. selector: The default value of the selector is "model".It can be override by writing this attribute.This attribute is optional.
3.extension: Sling Model needs to be exported as json.So extensions should be as “json”

Let's have a quick demo of Sling Model Exporter:

Jackson Exporter Options include:
Mapper Feature options
Serialization Feature options
Jackson Exporter Options help to manage the JSON response and control the output of the JSON

Let’s understand the Jackson Exporter Options With Example:

@ExporterOption(name = "MapperFeature.SORT_PROPERTIES_ALPHABETICALLY", value = "true")

By Default It’s value is false, then json response will be as it is.


When The value is true,then it sorts the json response alphabetically based on key.

Except this, MapperFeature and SerializationFeature exporter annotation has many more options.

Note: While the Apache Sling project provides the Jackson Exporter that serializes Sling Models to JSON, the Exporter framework also supports custom Exporters.

Applying Jackson Annotations:
Exporters implementations may also support annotations that can be applied inline on the Sling Model class, which can provide a finer level of control of how the data is exported.

Here we go, how to use Jackson annotations with Sling Model Exporter:

To get a detailed knowledge of all the Jackson annotations, go through this link


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.