April 29, 2020
Estimated Post Reading Time ~

How to Create and Consume Content using Apache Sling

Apache Sling is one of the core technologies used in Adobe Experience Manager (formerly known as Adobe CQ / Day CQ).

Sling started as an internal project at Day Software and taken up by Apache in September 2007. It’s the reason why some bundles/libraries are named like com.day.

The other important technologies include Apache Jackrabbit, Apache Felix, OSGi. We will discuss the Apache Sling in this article, and how to create a sample content and consume it.

Apache Sling is a web framework that uses a Java Content Repository (like Apache Jackrabbit) to store and manage content. Sling applications use either scripts or Java servlets to process HTTP requests in a RESTful way.

Sling in very simple terms could be described as a REST API for JCR. We can use http requests to manage content inside the repository. Sling provides a mechanism to render that content in different ways. We can use JSP, ESP scripts, Java (servlets, pojos, etc) in the Felix container to process requests and deliver content.

When a request is made for a particular node, Sling looks up for a property called sling:resourceType. This is used to look up the rendering scripts for the requested resource.

Apache Sling is:
  • a REST-based web framework
  • Content-driven, using JCR
  • Powered by OSGi framework
  • an Apache Open Source project
We shall see an example of how to start, create a node, and read the node’s content from Sling.
  1. Download Apache Sling Launchpad self-runnable jar from https://sling.apache.org/downloads.cgi. In my case, I have downloaded the latest jar org.apache.sling.launchpad-8.jar
  2. After the jar is downloaded, just start it as follows:java -jar org.apache.sling.launchpad-8.jar
  3. This starts the Sling embedded Web Server on port 8080 and writes application files into the sling folder found in the current working directory of your choice. You can check http://localhost:8080/system/console/bundles to see if the application is started properly or not. The credentials would be admin/admin. Here you would see different bundles in started state.
  4. Create content by giving the below cUrl command:
curl -u admin:admin -F "sling:resourceType=training/aem" -F "title=Sling for AEM" http://localhost:8080/content/myaemnode

We can expect a response as below:


Now check the content by using the below URLs from a web browser.

Content node in HTML format: http://localhost:8080/content/myaemnode.html


The same node in JSON format: http://localhost:8080/content/myaemnode.json



By aem4beginner

No comments:

Post a Comment

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