ruchika jain
ruchika jain

Reputation: 160

AEM 6.3 ResourceResolverFactory is null and throwing NullPointerException

Also tried this post AEM 6.3 - ResourceResolverFactory is null in Service and throwing LoginException in Sling Model class.

Interface

package org.employee.employee.core.admin;

import org.apache.sling.api.resource.ResourceResolver;

public interface CustomerService {
    public String getCustomerData();
}

Service Class ==> Getting resolverFactory null

package org.employee.employee.core.admin; 

import java.util.HashMap;
import java.util.Map;    
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype = true, immediate = true)
@Service
public class CustomerServiceImpl implements CustomerService {

    protected final Logger log = LoggerFactory.getLogger(this.getClass());

    @Reference
    private ResourceResolverFactory resolverFactory;

    public String getCustomerData() {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put(ResourceResolverFactory.SUBSERVICE, "CustomerService");
        ResourceResolver resolver = null;
        try {
            resolver = resolverFactory.getServiceResourceResolver(param);
            System.out.println(resolver);
        } catch (LoginException loginExcp) {
            log.error("Exception while getting resource resolver." + loginExcp);
        }
        return resolver.toString();
    }
}

Service Component is showing state=> Active

enter image description here

UserMapper

enter image description here

Upvotes: 0

Views: 1555

Answers (2)

Ramachandra A Pai
Ramachandra A Pai

Reputation: 417

Also I believe you should not use the admin user (as mentioned in the screenshot) to access services since Service mappings was developed to prevent unrestricted access to the repository.

Instead create a new System User and give the user only those permissions that are needed for the service.

To create a new system user you can follow the below steps:

  1. On your AEM instance, open http://server:port/crx/explorer/index.jsp
  2. Press the Log In link in the upper left corner of the screen and login as admin.
  3. Click on create system user and assign user ID as myServiceUser and intermediate path as system/MyModule.
  4. Assign permissions to the user using useradmin console.
  5. Add a configuration amendment to the ServiceUserMapper configuration.

Please refer this helpx for complete steps. https://helpx.adobe.com/in/experience-manager/6-3/sites/administering/using/security-service-users.html

Another SO answer that justifies this: ResourceResolverFactory getServiceResourceResolver throws Exception in AEM 6.1

This tool from ACS Commons can be quite helpful to ensure that service users exist. https://adobe-consulting-services.github.io/acs-aem-commons/features/ensure-service-users/index.html

Upvotes: 0

ruchika jain
ruchika jain

Reputation: 160

I was accessing it like

CustomerService customerService = new CustomerService();
String resolver = customerService.getCustomerData();

Which is not correct and Solved by

@Reference
private CustomerService customerService;
String resolver = customerService.getCustomerData();

Upvotes: 0

Related Questions