Expand my Community achievements bar.

Applications for the 2024-2025 Adobe Experience Manager Champion Program are open!
SOLVED

Node vs Resource in AEM

Avatar

Level 5

Hello Team, 

I am a beginner to AEM and I'm eager to learn and explore aem concepts. And i want to clarify that what is node & resource? Where we need to use node ? resource and which is better ? I see many terminalogies resourceResolver, adaptTo, adapter classes so this all comes under node & resource . If i understand this better i can use methods, variables & interfaces accordinly. 

 

Thanks.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @Tessa_learner1 , You will find this helpful to develop a base understanding of the terminologies and APIs available.
Understanding AEM Resources In 10 Minutes / Blogs / Perficient

NodeResource
Building block of JCR (Java Content Repository)Resources are pieces of content on which Sling acts upon.
Accessed by JCR APIs Accessed using Sling APIs
JCR Assumes - Everything is content (Nodes)Sling Assumes - Everything is Resource. Sling maintains a virtual tree of resources.
Write and Read operations to nodes is done using Session APIResourceResolver defines the service API which may be used to resolve Resource objects.

 

shubhanshu_singh_0-1686237316112.png

JCR API is a lower level API used to directly access and manipulate the underlying content repository. Apache Sling API is a higher-level web framework built on top of the JCR API.


The Resource and ResourceResolver interfaces are defined with a method adaptTo, which adapts the object to other classes. Using this mechanism the JCR session of the resource resolver calling the adaptTo method with the javax.jcr.Session class object. Likewise the JCR node on which a resource is based can be retrieved by calling the Resource.adaptTo method with the javax.jcr.Node class object.

 

Node node = resource.adaptTo(Node.class);

 

Using this you can switch between these two APIs and others. There are other set of APIs available to interact with content such as Page, Asset, PageManager etc.

"Where we need to use node ? resource and which is better"
As a general rule, unless you need the features available in the JCR API(Node), sticking with the Sling API will make your code more concise and less exception prone.

Good Reads
AEM Resource API. Resource API is most important… | by Imran Khan | Medium
Java™ API Best Practices in AEM | Adobe Experience Manager

View solution in original post

5 Replies

Avatar

Correct answer by
Community Advisor

Hi @Tessa_learner1 , You will find this helpful to develop a base understanding of the terminologies and APIs available.
Understanding AEM Resources In 10 Minutes / Blogs / Perficient

NodeResource
Building block of JCR (Java Content Repository)Resources are pieces of content on which Sling acts upon.
Accessed by JCR APIs Accessed using Sling APIs
JCR Assumes - Everything is content (Nodes)Sling Assumes - Everything is Resource. Sling maintains a virtual tree of resources.
Write and Read operations to nodes is done using Session APIResourceResolver defines the service API which may be used to resolve Resource objects.

 

shubhanshu_singh_0-1686237316112.png

JCR API is a lower level API used to directly access and manipulate the underlying content repository. Apache Sling API is a higher-level web framework built on top of the JCR API.


The Resource and ResourceResolver interfaces are defined with a method adaptTo, which adapts the object to other classes. Using this mechanism the JCR session of the resource resolver calling the adaptTo method with the javax.jcr.Session class object. Likewise the JCR node on which a resource is based can be retrieved by calling the Resource.adaptTo method with the javax.jcr.Node class object.

 

Node node = resource.adaptTo(Node.class);

 

Using this you can switch between these two APIs and others. There are other set of APIs available to interact with content such as Page, Asset, PageManager etc.

"Where we need to use node ? resource and which is better"
As a general rule, unless you need the features available in the JCR API(Node), sticking with the Sling API will make your code more concise and less exception prone.

Good Reads
AEM Resource API. Resource API is most important… | by Imran Khan | Medium
Java™ API Best Practices in AEM | Adobe Experience Manager

Avatar

Community Advisor

Hello @Tessa_learner1 

Node: In AEM, a node is a unit of content in the JCR repository. It can be a page, component, asset, or any other type of content.

Details: https://medium.com/@toimrank/aem-node-api-a3ba42c148b4 

 

Resource: A resource is a reference (logical reference) to a node in the JCR repository. 

Deatils: https://medium.com/@toimrank/aem-resource-api-2dcb971060b8 

 

The main difference between a node and a resource is that a node is a physical entity in the JCR repository, while a resource is a logical reference to a node. You should use nodes when you need to interact with the JCR repository directly. For example, you would use nodes to create, update, or delete content. You would also use nodes to access the properties of a node. You should use resources when you need to interact with the JCR repository indirectly. For example, you would use resources to render a page, access an asset, or perform a search. [Generated]

 

ResourceResolver: A class that provides access to the JCR repository and the Sling environment. Resource resolver can get in 2 ways(requestResourceResolver, serviceResourceResolver).
Request Resource Resolver: Use when you only want of user will access only where he has permission. Like content, dam.

Service Resource Resolver: To use this there needs to use a special user called a service user. Use it when you need to access some special node. Like etc. Details: https://aemhints.com/2020/11/08/service-users-mapper-aem/ 

AdaptTo: A method that can be used to convert a resource to another type of object

Adapter classes: Classes that provide adapters for different types of objects

Details: https://experienceleague.adobe.com/docs/experience-manager-65/developing/platform/sling-adapters.htm... 

http://www.wemblog.com/2013/07/how-to-use-adapters-in-adobe-cq-aem.html 

 

Hope it helps you.

Thanks

Avatar

Community Advisor

Hello @Tessa_learner1 

 

A node represents a fine-grained level of access in AEM, particularly in relation to the JCR (Java Content Repository).

 

On the other hand, the term "resource" is commonly used in the context of Sling. When implementing functionalities in AEM, discussions often revolve around resources, such as pages and assets. A resource can be conveniently adapted to these units, like pages or assets, and relevant APIs can be used to retrieve or modify their properties.

Resources offer various APIs that provide default values and handle exceptions more effectively when a value or child does not exist. These APIs closely align with how AEM developers typically code, resulting in more manageable and less fragile code. You would notice benefits on level of:

  1. Adapting and Reusing Resource-centric APIs (Page/Assets/tags etc)
  2. Exception handling
  3. Accessing properties/hierarchy
  4. Event handling

 

On API level, please refer to differences on https://myprogressivelearning.wordpress.com/2016/01/23/aem-sling-api-vs-jcr-api/

https://cqdump.joerghoh.de/2012/11/13/cq-coding-patterns-sling-vs-jcr-part-2/ 

 

When to use Node: 

Using a lower-level API generally leads to improved performance. The JCR (Java Content Repository) directly interacts with the content repository, whereas Sling follows RESTful principles for resource resolution, which can introduce performance overhead.

In summary, when accessing nodes, it is advisable to use the Sling API unless specific circumstances, such as severe performance degradation, warrant using the JCR API.


Aanchal Sikka

Avatar

Community Advisor

@Tessa_learner1 In short,  Node is like a unit of the JCR repository and resource is something that sling uses to access Nodes. JCR APIs act on the Node and Sling APIs use the resource.

 

The Sling API works at a higher, more abstract level than the JCR API. This allows your code to be more reusable and independent of the underlying storage. This makes it easier to include external virtual data via the ResourceProvider mechanism if needed.

 

resourceResolver defines the API which may be used to resolve Resource objects and adapt to whichever object you wish to "adabtTo" and work with such resources like creating, editing or updating them.