top of page
  • Writer's pictureHeena

Sling Model Exporters in AEM: An Overview

Updated: May 2


  1. Sling model exporter in AEM enables you to export resource in JSON format. It is implemented by adding @Exporter annotation to the Sling Model.

  2. Without Custom Sling Exporter class, It will return all the node properties using OOTB Sling Model Exporter while Custom Sling Exporter exports properties defined in Sling Model.

  3. Exporters implementations may also support annotations that can be applied inline on the Sling Model, that can provide a finer level of control how the data is exported.


When to use Sling Exporter? Sling Model Exporter is perfect for leveraging Sling Models that already contain business logic that support HTML renditions via HTL (or formerly JSP), and expose the same business representation as JSON for consumption by programmatic Web services or JavaScript applications.


Let's implement a Sling Exporter on Sling Model we created here.


1. To implement Sling Exporter, add following annotation before class implementation:

@Exporter(extensions = { "json" }, name = "jackson", options = { @ExporterOption(name = "MapperFeature.SORT_PROPERTIES_ALPHABETICALLY", value = "true") }, selector = "test")

name = the registered name of the exporter extensions = the extensions this exporter is registered to options = Following are Jackson Feature options:

Serialization Features: http://static.javadoc.io/com.fasterxml.jackson.core/jackson-databind/2.8.5/com/fasterxml/jackson/databind/SerializationFeature.html selector = defaults to "model", can override as needed. This is helpful if a single resource needs 2 different JSON renditions


2. To add SlingHttpServletRequest.class as an adaptable of the Sling Model since exporter is invoked directly via a request, and not via a resource.


3. To add the resourceType if you want Sling to "naturally" expose this model as the exporter for a Resource.


@Model(adaptables = { SlingHttpServletRequest.class}, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL, resourceType = "aemblog/components/my-first-sling-model")

@Exporter(extensions = { "json" }, name = "jackson", options = { @ExporterOption(name = "MapperFeature.SORT_PROPERTIES_ALPHABETICALLY", value = "true") })

public class MyFirstSlingModel {

	@ValueMapValue
	private String title;

	@PostConstruct
	protected void init() {

		title = title.concat(" Updated By Sling Model");

	}
	public String getTitle() {
		return title;
	}
}

Let's try test the exporter now. Since we didn't pass selector attribute to Exporter annotation, we are passing default selector model in the url.

sling exporter output




That's all for today! If you've found this blog post informative or helpful, I’d greatly appreciate it if you could give it a like. It keeps me motivated 💛


Enjoying my ad-free blog? Support by buying me a coffee! I've kept this space ad-free, sponsoring it myself to maintain its purity. Your contribution would help keep the site afloat and ensure quality content. Thanks for being part of this ad-free community.

210 views

Related Posts

See All

コメント


bottom of page