What is Resourceresolver in AEM | Community
Skip to main content
Level 4
September 6, 2022
Solved

What is Resourceresolver in AEM

  • September 6, 2022
  • 4 replies
  • 10332 views

Hell all, 

I'm using resource resolver in my code. But it throws an error. Also, i want to know, what is resource resolver, and why it is used. And what is the correct syntax for resource resolver.  Can anyone help me with this ? 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by HeenaMadan

The ResourceResolver defines the service API which may be used to resolve Resource objects. The resource resolver is available to the request processing servlet through the SlingHttpServletRequest.getResourceResolver() method.

Refer https://sling.apache.org/apidocs/sling6/org/apache/sling/api/resource/ResourceResolver.html

https://unlocklearning.in/resource-resolver-in-aem/

 

Share logs what is the error you are getting while using resource resolver and also snippet of the code base.

 

4 replies

Sachin_Arora_
Community Advisor
Community Advisor
September 6, 2022

AEM is build on an open-source Restful Web framework i.e. Sling. Resources are piece of content on which Sling acts i.e. /apps, /content all are resource in terms of Sling Framework. So to get the resource java object, ResourceResolver API helps you in it. 

There are various ways you can create the resource resolver object :

Links :

 

Shiv_Prakash_Patel
Community Advisor
Community Advisor
September 6, 2022

Hi @tessa_learner1 ,

Please check out this article -https://unlocklearning.in/resource-resolver-in-aem/ 

Hope this could help you !!!

Thanks

Shiv

Shiv Prakash
B_Sravan
Community Advisor
Community Advisor
September 6, 2022

Hi @tessa_learner1 ,

To address your error, I might need your code snippet to check.


In simple words, a Resource Resolver is a basically an interface between the objects in the repository (Sling considers them as resources) and the code you write. Since the interface api helps to access the resources whilst having all the access related factors (resolution), it is hence named Resource Resolver.

Correct syntax of using Resource resolver:

In a Sling Servlet, resource resolver is available to the request processing servlet through the SlingHttpServletRequest.getResourceResolver()  method. 

In a Sling Model, you can have the instance of resource resolver through the injector specific annotation "@SlingObject"

@SlingObject
    private ResourceResolver resourceResolver;

In other way, you can also get the resource resolver through ResourceResolverFactory.

@Reference 
private transient ResourceResolverFactory resourceResolverFactory; 

ResourceResolver resourceResolver = resourceResolverFactory.getServiceResourceResolver(ResourceResolverFactory.SUBSERVICE, "service-user-name"); 

In the above code, the parameters passed in the getServiceResourceResolver are sub service constant and service-user-name, which is the system user id who have access to the resources.

to know more on how to create a system/service user, please refer this official document or follow this simple video 

Level 4
September 7, 2022

Hi @b_sravan  Thank you for the references. It's really helpful. There was some syntax error in my code. Now it's working fine. This is the working code. 

 

package testing.core.models;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
//import javax.annotation.Resource;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.adapter.Adaptable;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.Self;

import com.day.cq.tagging.Tag;
import com.day.cq.tagging.TagManager;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;

@Model(adaptables=Resource.class)

public class TagManagerModel {

@Self
Resource resource;
ResourceResolver resourceResolver=resource.getResourceResolver();
//ResourceResolver resourceResolver=
List<String> tagNames;
@PostConstruct

public void init() {

PageManager pm = resource.getResourceResolver().adaptTo(PageManager.class);
Page containingPage = pm.getContainingPage (resource);
ValueMap pageProperties = containingPage.getProperties();
String[] tags = pageProperties.get("cq:tags", new String[0]);
tagNames = new ArrayList<>();
TagManager tagManager = resourceResolver.adaptTo(TagManager.class);
for(String tagList : tags){
Tag tag = tagManager.resolve(tagList);
tagNames.add(tag.getTitle());
}
}

}

HeenaMadan
Community Advisor and Adobe Champion
HeenaMadanCommunity Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
September 6, 2022

The ResourceResolver defines the service API which may be used to resolve Resource objects. The resource resolver is available to the request processing servlet through the SlingHttpServletRequest.getResourceResolver() method.

Refer https://sling.apache.org/apidocs/sling6/org/apache/sling/api/resource/ResourceResolver.html

https://unlocklearning.in/resource-resolver-in-aem/

 

Share logs what is the error you are getting while using resource resolver and also snippet of the code base.

 

PunitMuddebihal
June 15, 2024

Very useful.