Expand my Community achievements bar.

SOLVED

What is Resourceresolver in AEM

Avatar

Level 5

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 ? 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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.

 

View solution in original post

5 Replies

Avatar

Community Advisor

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 :

 

Avatar

Community Advisor

Hi @Tessa_learner1 ,

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

Hope this could help you !!!

Thanks

Shiv

Shiv Prakash

Avatar

Community Advisor

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 

Avatar

Level 5

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());
}
}

}

Avatar

Correct answer by
Community Advisor

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.