Showing posts with label Sling model implementation with AEM 6.2. Show all posts
Showing posts with label Sling model implementation with AEM 6.2. Show all posts

Thursday 14 February 2019

What is Sling Model Exporters?

Sling Models

Sling Models are business objects that represents sling resources or sling requset objects in AEM. In other way, Sling Models let you map Java objects to Sling resources.

Sling Models Exporter

Sling Model Exporters helps to export the model as a different Java object (serialized into a different format such as JSON) by adding annotations to Sling Model. The model is programmatically exported by calling the ModelFactory method exportModel().

Jackson exporter
Jackson exporter which is used in Sling allows to convert Sling Model into a Java Map object. Jackson JSON exporter helps exporting Sling Models as JSON objects, which can be accessible from other third party web service applications, Java script application etc.

Sling Model Exporter is available from Sling Models version v1.3.0. 

Pictorial representation of Sling Model Vs Sling Model Exporter

Click on image to see it big

Can I use Sling Model Exporters with versions prior to AEM 6.3?

Yes AEM 6.3 onward no extra dependencies required to use Sling Model Exporters.

But versions prior to AEM 6.3 requires installation of below bundled packages from,
Sling > Downloads
  • Models API 1.3.0+
  • Models Implementation 1.3.0+
  • Models Jackson Exporter 1.0.0
  • AEM 6.2 Communities/Livefyre - FP2

How to add exporter framework to an existing sling model?

1) Add a resource type to a model,

Add below line in @Model code block of Sling Model.

resourceType = "[The type of the resourcse which we are requesting]";

2) Add a new annotation,

@Exporter ( name = "jackson", extensions = "json")

Associate a model class with a resource in SlingDocumentation here

After building the code, invoke the resource with selector model.json

This will bring back the response in JSON format and the response is picked from the getter methods in the sling model class.

Sling Exporter Documentation

Demo Video


Read More:

Sling Model Vs WCMUsePOJO

Thursday 1 June 2017

Sling models - Adobe Recommended Way Of Object Binding

Sling Models
Sling Models are widely used with in AEM now. They are simple POJO classes(support both class and interface) mapped automatically with Sling Objects( resources, also request objects). Example use cases in AEM are they allow us to access jcr node property values directly into java classes. Also helps to wire the OSGi Services. Sling Models uses annotations & annotations Mark a class as adaptable via Sling Models.

Why sling models:

  • Adobe recommended Sling Models as the best practice.
  • Provides convenient injection annotations for data retrieval.
  • Pure POJO classes. Entirely annotation driven.
  • Less code to write & easy to extend from other Sling Models.
  • Quite simple setup for unit testing.
  • Flexibility of smooth integration with JSP or sightly.

Two major differences with WCM Use class and Sling Model

  1. WCM Use classes expect you to overwrite the activate() method, But Sling Models provides @PostConstruct annotation, where the init method will be called.
  2. ECM Use classes use Felix annotation @Reference to reference the OSGI service, whereas in Sling Models, you will use @Inject or @OSGiService

Below given the complete list of annotation references

  • @Model - declares a model class or interface
  • @Inject - marks a field or method as injectable
  • @Named - declare a name for the injection (otherwise, defaults based on field or method name).
  • @Optional - marks a field or method injection as optional
  • @Source - explictly tie an injected field or method to a particular injector (by name). Can also be on other annotations.
  • @Filter - an OSGi service filter
  • @PostConstruct - methods to call upon model option creation (only for model classes)
  • @Via - use a JavaBean property of the adaptable as the source of the injection
  • @Default - set default values for a field or method
  • @Path - only used together with the resource-path injector to specify the path of a resource

While working with AEM latest versions, do we need any update for Sling models?
You can check the version of Sling Model from AEM Console.

If you are working with AEM 6.2, download the latest Sling Models API and Implementation bundles from Sling and then manually upload them to AEM bundles console, because AEM 6.2 has Sling Models API and Implementation version 1.2. If you are workig with AEM 6.3 you dont have to do this step.

In future when Sling new major release available, you can still manually import them into your 6.3 server.( Ensure to check Adobe’s documentation for additional dependency package to be installed.)

Sample Code: Sling Model Vs Sightly

// @Model declares a model class or interface
@Model(adaptables = Resource.class) public class MyCustomClass {
// @Inject marks a field or method as injectable
// age can be empty
@Inject @Optional
private String age;
// we expected always an firstName-property
private String firstName;
// OSGi Externalizer Service
private Externalizer externalizer;

    // Executed after the class is created
    @PostConstruct  protected void init() {
     //init here  


How sightly calls the Sling Model class

<div data-sly-use.customClass=customsite.customproject.MyCustomClass>
${ customClass.firstName }