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)
Solved! Go to Solution.
Views
Replies
Total Likes
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.
Adapters are the interfaces or classes that a Sling Model can be adapted to. Essentially, they define the type of the resulting model object.
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...
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.
Adapters are the interfaces or classes that a Sling Model can be adapted to. Essentially, they define the type of the resulting model object.
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...
Views
Likes
Replies
Views
Likes
Replies