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:
Sling Model without Exporter:
Jackson Exporter Options include:
To get a detailed knowledge of all the Jackson annotations, go through this link
If you have any query or suggestion then kindly comment or mail us at sgaem.blog02@gmail.com
Hope it will help you guys !!
Thanks and Happy Learning .
| Fig - Dialog Values of Hero Component |
Sling Model without Exporter:
To enable sling model as an exporter,the @Exporter annotation is being used.
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. 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”
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:
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:
By Default It’s value is false, then json response will be as it is.
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:
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, that can provide a finer level of control how the data is exported.
Here we go, how to use Jackson annotations with Sling Model Exporter:
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
If you have any query or suggestion then kindly comment or mail us at sgaem.blog02@gmail.com
Hope it will help you guys !!
Thanks and Happy Learning .