Expand my Community achievements bar.

Adobe Summit 2025: AEM Session Recordings Are Live! Missed a session or want to revisit your favorites? Watch the latest recordings now.
SOLVED

AEM - Sling Model

Avatar

Level 1

Hi @everyone
What is the difference between 'Adaptables' and 'Adapters' in Adobe Experience Manager (AEM) Sling Models? I would appreciate any insights on the distinctions between these two concepts and how they are applied in the context of Sling Models in AEM. Specifically, how do Adaptables and Adapters function differently, and how do they affect the design and implementation of Sling Models?

@Model(adaptables = { Resource.class, SlingHttpServletRequest.class }, adapters = ComponentExporter.class)



1 Accepted Solution

Avatar

Correct answer by
Level 7

Hi @YaminiKolhe 
Happy to see you have raised this. This is basic yet very important discussion, 

Adaptable defines the sources from which a model can be created,
Adapters define the interfaces or classes the model can be adapted to

Let me take you through some examples:
Adaptable: The most common adaptables in AEM are Resource and SlingHttpServletRequest.

  • Resource: Represents a resource in the JCR (Java Content Repository). It's often used when you want to adapt a model from a specific node in the repository.
  • SlingHttpServletRequest: Represents an HTTP request in Sling. It's used when you need to adapt a model from the context of a request, which might include parameters, headers, and other request-specific data.Suppose we have a Sling Model that represents a component in AEM. We want this model to be adaptable from both a Resource and a SlingHttpServletRequest, and we want it to implement ComponentExporter so it can be exported as JSON.

Adapters are the interfaces or classes that a Sling Model can be adapted to. Essentially, they define the type of the resulting model object.

  • ComponentExporter: This is a common adapter in AEM, especially when working with the AEM Core Components. It allows the model to be used as a JSON exporter, making it suitable for headless CMS scenarios.

Suppose we have a Sling Model that represents a component in AEM. We want this model to be adaptable from both a Resource and a SlingHttpServletRequest, and we want it to implement ComponentExporter so it can be exported as JSON.

@Model(
    adaptables = { Resource.class, SlingHttpServletRequest.class },
    adapters = ComponentExporter.class,
    defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
)
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class MyComponentModel implements ComponentExporter {

    @ValueMapValue
    private String title;

    @ValueMapValue
    private String description;

    public String getTitle() {
        return title;
    }

    public String getDescription() {
        return description;
    }

    @Override
    public String getExportedType() {
        return "project/components/mycomponent";
    }
}

Adaptables: The @Model annotation specifies that this model can be adapted from both Resource and SlingHttpServletRequest. This means you can create an instance of MyComponentModel from either a resource in the repository or an HTTP request.
Adapters: The model is adapted to ComponentExporter, which means it implements the ComponentExporter interface. This allows the model to be used in contexts where JSON export is needed, such as in headless CMS scenarios.

You can also go through: 
https://experienceleague.adobe.com/en/docs/experience-manager-65/content/implementing/developing/pla...
https://aasishjain.medium.com/adapter-class-in-sling-model-d5d71b058223 
https://www.blueacornici.com/blog/understanding-the-sling-adapter-framework-in-aem 
https://medium.com/@ucgorai/best-practices-for-writing-sling-models-in-aem-c6ff16a0fd00 
https://medium.com/@angadi.saa/aem-sling-models-which-one-to-use-as-adoptables-resource-or-slinghtps...

View solution in original post

1 Reply

Avatar

Correct answer by
Level 7

Hi @YaminiKolhe 
Happy to see you have raised this. This is basic yet very important discussion, 

Adaptable defines the sources from which a model can be created,
Adapters define the interfaces or classes the model can be adapted to

Let me take you through some examples:
Adaptable: The most common adaptables in AEM are Resource and SlingHttpServletRequest.

  • Resource: Represents a resource in the JCR (Java Content Repository). It's often used when you want to adapt a model from a specific node in the repository.
  • SlingHttpServletRequest: Represents an HTTP request in Sling. It's used when you need to adapt a model from the context of a request, which might include parameters, headers, and other request-specific data.Suppose we have a Sling Model that represents a component in AEM. We want this model to be adaptable from both a Resource and a SlingHttpServletRequest, and we want it to implement ComponentExporter so it can be exported as JSON.

Adapters are the interfaces or classes that a Sling Model can be adapted to. Essentially, they define the type of the resulting model object.

  • ComponentExporter: This is a common adapter in AEM, especially when working with the AEM Core Components. It allows the model to be used as a JSON exporter, making it suitable for headless CMS scenarios.

Suppose we have a Sling Model that represents a component in AEM. We want this model to be adaptable from both a Resource and a SlingHttpServletRequest, and we want it to implement ComponentExporter so it can be exported as JSON.

@Model(
    adaptables = { Resource.class, SlingHttpServletRequest.class },
    adapters = ComponentExporter.class,
    defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
)
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class MyComponentModel implements ComponentExporter {

    @ValueMapValue
    private String title;

    @ValueMapValue
    private String description;

    public String getTitle() {
        return title;
    }

    public String getDescription() {
        return description;
    }

    @Override
    public String getExportedType() {
        return "project/components/mycomponent";
    }
}

Adaptables: The @Model annotation specifies that this model can be adapted from both Resource and SlingHttpServletRequest. This means you can create an instance of MyComponentModel from either a resource in the repository or an HTTP request.
Adapters: The model is adapted to ComponentExporter, which means it implements the ComponentExporter interface. This allows the model to be used in contexts where JSON export is needed, such as in headless CMS scenarios.

You can also go through: 
https://experienceleague.adobe.com/en/docs/experience-manager-65/content/implementing/developing/pla...
https://aasishjain.medium.com/adapter-class-in-sling-model-d5d71b058223 
https://www.blueacornici.com/blog/understanding-the-sling-adapter-framework-in-aem 
https://medium.com/@ucgorai/best-practices-for-writing-sling-models-in-aem-c6ff16a0fd00 
https://medium.com/@angadi.saa/aem-sling-models-which-one-to-use-as-adoptables-resource-or-slinghtps...