To enable JSON export on any component after defining a Sling Model, you need to annotate the Sling Model interface with @Exporter annotation. This annotation is part of the Sling Model Exporter framework which allows Sling Models to be exported in JSON format.
Here is the detailed step-by-step explanation:
Define the Sling Model: Define your Sling Model with the necessary annotations and methods. For example:
@Model(adaptables = Resource.class)
public class MyModel {
@Inject
private String title;
public String getTitle() {
return title;
}
}
Annotate with @Exporter: Annotate the Sling Model class with @Exporter to enable JSON export. The @Exporter annotation configures the model to be exported as JSON.
@Model(adaptables = Resource.class)
@Exporter(name = "jackson", extensions = "json")
public class MyModel {
@Inject
private String title;
public String getTitle() {
return title;
}
}
Access the JSON Output: Once annotated, the JSON representation of the model can be accessed through the .model.json extension. For example, if your component is at /content/mysite/en, you can access the JSON output at:
http://localhost:4502/content/mysite/en.model.json
This approach leverages the Sling Model Exporter framework to seamlessly convert Sling Models to JSON format, making it easy to integrate with front-end frameworks and other systems that consume JSON data.
References:
Adobe Sling Model Exporter
AEM Core Components JSON Export