Reputation: 160
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
UserMapper
Upvotes: 0
Views: 1555
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:
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
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